一、一维数组
int[] arr1;//声明
arr1 = new int[] {
1,2,3,4};//赋值
//合并声明和初始化
int[] arr2 = new int[] {
4,5,6,7};
//或 int arr2[] = new int[]{
4,5,6,7}
//或 int[] arr2 = {
4,5,6,7}//类型推断
(2)动态初始化:数组的初始化和数组元素的赋值分开进行
String[] arr3 = new String[3];
System.out.println(arr1[1]);
System.out.println(arr3.length);
for(int i = 0;i < arr.length;i++){
System.out.println(arr[i]);
}
//方式一
int[][] arr1 = new int[4][3];
//或
int[] arr1[] = new int[4][3];
int arr1[][] = new int[4][3];
//方式二
int[][] arr2 = new int[3][];
arr2[0] = new int[3];
arr2[1] = new int[2];
arr2[2] = new int[1];
(2)静态初始化
int[][] arr3 = new int[][]{
1,2,3,4};
System.out.println(arr3[0][2]);
System.out.println(arr3.length);
System.out.println(arr3[i].length);
for (int i = 0;i < arr3.length;i++){
for (int j = 0;j < arr3[i].length;j++){
System.out.println(arr3[i][j]);
}
}
int[] arr1,arr2;
arr1 = new int[]{
2,3,5,7,11,13};
//1.赋值:类似于创建一个快捷方式
arr2 = arr1;
for (int i = 0;i < arr1.length;i++){
System.out.print(arr1[i] + '\t');
}
//修改arr2中偶索引位置上的元素
for (int i = 0;i < arr2.length;i++){
if (i % 2 == 0){
arr2[i] = i;
}
System.out.print(arr1[i] + '\t');
System.out.println();
System.out.print(arr2[i] + '\t');
}
//复制
arr2 = new int[arr1.length];
for (int i = 0;i < arr1.length;i++){
arr2[i] = arr1[i];
}
System.out.println(arr1 + '\t');//地址值
System.out.println(arr2 + '\t');//地址值
for (int i = 0;i < arr2.length;i++) {
if(i % 2 == 0) {
arr2[i] = i;
}
}
for (int i = 0;i < arr1.length;i++) {
System.out.print(arr1[i] + "\t");//0 3 2 7 4 13
}
String[] arr3 = new String[]{
"晴天","阴天","多云","下雨","下雪"};
//方式一:
for (int start = 0,int end = arr3.length;start < end;start++,end--){
String temp = arr3[start];
arr3[start] = arr3[end];
arr3[end] = temp;
}
//方式二:
for (int i = 0;i < arr3.length / 2;i++){
String temp = aarr3[i];
arr3[i] = arr3[length - 1 -i];
arr3[length - 1 -i] = temp;
}
//1.线性查找:普遍适用性
String str = "下雨";
boolean isFlage = true;
for (int i = 0;i < arr3.length;i++){
if (arr3[i].equals(str)){
System.out.print("找到了,位置在:" + i);
isFlage = false;
break;
}
if(isFlage){
System.out.println("没有找到指定内容");
}
}
//2.二分法查找:数组要有序
int[] arr3 = new int[] {
-1,0,1,2,3,4,5,6,7,8};
int num = 4;
boolean isFlage1 = true;
int head = 0;//首索引位置
int end = arr3.length - 1;//尾索引位置
while (head <= end) {
int mid = (head + end) / 2;
if (arr3[mid] == num) {
System.out.println("找到了,索引位置为:" + mid);
isFlage1 = false;
break;
} else if (arr3[mid] > num) {
end = mid - 1;
} else {
head = mid + 1;
}
}
if (isFlage1) {
System.out.println("没有找到");
}
冒泡排序
// 冒泡排序
int[] bubble = new int[]{
34,75,54,23,12,14,9,79};
//从小到大排列
for (int i = 0;i < bubble.length;i++){
for (int j = 0;j < bubble.length - 1 - i;j++){
if(bubble[j] > bubble[j + 1]){
int temp = bubble[j];
bubble[j] = bubble[j + 1];
bubble[j + 1] = temp;
}
}
}
for (int i = 0;i < bubble.length;i++){
System.out.print(bubble[i] + '\t');
}
四、Arrays工具类的使用
//1.数组角标越界异常:ArrayIndexOutOfBoundsException
for(int i = 0;i <= arr.length;i++) {
System.out.println(arr[i]);
}
System.out.println(arr[5]);
System.out.println(arr[-1]);
//2.空指针异常:NullPointerException
//举例1:
int[] arr1 = new int[4];
arr1 = null;
System.out.println(arr1[0]);
//举例2:
int[][] arr2 = new int[3][];
System.out.println(arr2[1][0]);
//举例3:
String[] arr3 = new String[3];
System.out.println(arr3[0].toString());
六、随机数的生成
如何生成一个随机数?