0. 数组的用法
数组的初始化 前面永远是空的
数组初始化完成,数组的长度是固定的
int[] arr = new int[]{1, 2, 3};
int[] arr2 = new int[10];
String[] str = new String[10];
1. 求数组的长度
System.out.println(arr.length);
2. 遍历数组
char c = new char[]{'C', 'h', 'i', 'n', 'a', ' ', 'C', 'i', 't', 'i', 'c', ' ', 'B', 'a', 'n', 'k'};
for(int i = 0; i < c.length; i++){
System.out.print(c[i]);
}
for (char value : c) {
System.out.print(value);
}
System.out.println();
String[] str = new String[]{"China", " Citic", " Bank"};
for(String value : str){
System.out.print(value);
}
3. 2维数组
2维数组的初始化
2维数组内部可以长度不一致,缺少的元素直接补0,但统计每行长度的时候,0不算在内,以具体数字为主
int[][] a, b;
int[] c[], d[];
int[][] array = new int[2][3];
array[0][0] = 1;
array[0][1] = 2;
array[1][0] = 3;
array[1][1] = 4;
array[1][2] = 5;
或者是初始化
int[][] arr = {{1, 2, 3}, {4, 5, 6}};
int[][] arr2 = int[][]{{4, 5, 6}, {7, 8, 9}};
4. 2微数组的遍历
for(int[] arr : array){
for(int k : arr){
System.out.println(k);
}
}
System.out.println("转化为字符串格式:" + Arrays.deepToString(array));
5. 打印杨辉三角
打印10行杨辉三角
1. 第一行有一个元素,第n行有n个元素
2. 每一行的第一个元素和最后一个元素都是1
3. 从第三行开始,对于非第一个元素和最后一个元素,即
yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j]
public class yanghuisanjiao {
public static void main(String[] args) {
int[][] yanghui = new int[10][];
for(int i = 0; i < yanghui.length; i++){
yanghui[i] = new int[i + 1];
yanghui[i][0] = 1;
yanghui[i][i] = 1;
for(int j = 1; j < yanghui[i].length - 1; j++)
yanghui[i][j] = yanghui[i - 1][j - 1] + yanghui[i - 1][j];
}
System.out.println("打印前10行的杨辉三角:");
for(int[] arr : yanghui){
for(int i : arr){
System.out.print(i + " ");
}
System.out.println();
}
}
}
6. 数组的工具
package com.company;
import java.util.Arrays;
public class arraytools {
public static void main(String[] args){
int[] arr1 = {1,2,3,4};
int[] arr2 = {1,3,2,4};
boolean isEquals = Arrays.equals(arr1, arr2);
System.out.println("这两个数组是否相等:" + isEquals);
System.out.println(Arrays.toString(arr1));
Arrays.fill(arr1, 10);
System.out.println(Arrays.toString(arr1));
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));
int[] arr3 = new int[]{-98, -34, 2, 34, 54, 65, 79, 100, -2, 0, 20, 45};
Arrays.sort(arr3);
int index = Arrays.binarySearch(arr3, 100);
System.out.println(index);
if(index >= 0){
System.out.println("找到了,在第" + index + "位!");
} else{
System.out.println("没有找到!");
}
}
}
7.数组中算法的考察
package com.company;
import com.sun.org.apache.bcel.internal.generic.SWAP;
import com.sun.org.apache.xerces.internal.dom.DeferredAttrImpl;
import com.sun.org.apache.xpath.internal.axes.ReverseAxesWalker;
import com.sun.xml.internal.ws.message.stream.StreamHeader12;
import sun.security.util.Length;
import java.nio.file.FileSystemLoopException;
import java.util.Arrays;
public class algorithmTest {
public static void main(String[] args){
int[] arr = new int[10];
for(int i = 0; i < arr.length; i++)
arr[i] = (int) (Math.random() * (99 - 10 + 1) + 10);
for(int i : arr)
System.out.print(i + " ");
System.out.println();
int maxValue = arr[0];
for(int v : arr)
if(v > maxValue)
maxValue = v;
System.out.println("arr数组的最大值是:" + maxValue);
String[] str = new String[]{"JJ", "DD", "MM", "YY", "NN"};
System.out.println("str字符串数组中存储的内容是:" + Arrays.toString(str));
String[] str1 = new String[str.length];
System.arraycopy(str, 0, str1, 0, str.length);
String temp;
for(int i = 0, j = str1.length - 1; i < j; i++, j--){
temp = str1[i];
str1[i] = str1[j];
str1[j] = temp;
}
System.out.println("str1字符串数组中存储的内容是:" + Arrays.toString(str1));
String dest = "FF";
boolean flag = false;
for (String s : str)
if(s.equals(dest)){
System.out.println("找到了,是" + dest);
flag = true;
break;
}
if(!flag)
System.out.println("没有找到" + dest);
int[] bubble = {23, 0, 9, -2, -5, 24, 4, -8, 4};
for(int i = bubble.length - 1; i > 1; i--){
for(int j = 0; j < i; j++){
if(bubble[j] > bubble[j + 1]){
int t = 0;
t = bubble[j];
bubble[j] = bubble[j + 1];
bubble[j + 1] = t;
}
}
}
System.out.println("冒泡排序算法的结果是:" + Arrays.toString(bubble));
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)