# Java中数组的进阶学习

## 数组进阶

1.二维数组的初始化

int [ ][ ] = new int [ ] [ ] {{1,2,3},{4,5},{6,7,8}}

String [ ] [ ] arr2 = new String [ 3 ] [ 2]
String [ ] [ ] arr3 = new String [ 3 ] [ ]
2.题目
//创建一个长度为6的int型数组，要求数组元素的值都在1到30之间，且是随机赋值。同时，要求元素的值各不相同。

package zifuchuan;

import java.util.Random;

//创建一个长度为6的int型数组，要求数组元素的值都在1到30之间，且是随机赋值。同时，要求元素的值各不相同。
public class Test {
public static void main(String[] args) {
int []arr = new int[6];

for (int i = 0; i <arr.length ; i++) {
arr[i] = (int)(Math.random()*30) + 1;
for (int j = 0; j <i ; j++) {
if (arr[i] == arr[j]){
i--;
break;
}
}
}

for (int temp:arr) {
System.out.println(temp);
}
}

}


3.数组的复制和反转

package zifuchuan;

public class Test {
public static void main(String[] args) {
int [] arr1 = {1,2,3,4,5,6};
//数组的复制
int [] arr2 = new int[arr1.length];
for (int i = 0; i <arr1.length ; i++) {
arr2[i] = arr1[i];
}

/*        for (int temp:arr2) {
System.out.println(temp);
}*/

//数组的反转

//方法一
/*        for (int i = 0; i < arr1.length / 2 ; i++) {
int temp = arr1[i];
arr1[i] = arr1[arr1.length -1 - i];
arr1[arr1.length - 1 - i] = temp;
}*/

//方法二
for (int i = 0,j = arr1.length -1 ; i < j ; i++,j--) {
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}

for (int temp:arr1) {
System.out.println(temp);
}
}
}


4.Arrays工具类

package zifuchuan;

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
int [] arr = new int[]{2,4,15,28,35};
int a = Arrays.binarySearch(arr,28);
System.out.println(a);
}
}


package zifuchuan;

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
int [] arr = new int[]{36,1,67,13,24};
Arrays.sort(arr);
for (int temp:arr) {
System.out.println(temp);
}
}
}


package zifuchuan;

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
int [] arr = new int[]{36,1,67,13,24};
System.out.println(Arrays.toString(arr));//[36, 1, 67, 13, 24]

}
}


package zifuchuan;

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
int [] arr = new int[]{36,1,67,13,24};
int arr1 [] = Arrays.copyOf(arr,8);
System.out.println(Arrays.toString(arr1));
}
}


package zifuchuan;

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
int [] arr = new int[]{36,1,67,13,24};
int [] arr1 = arr;
boolean a = Arrays.equals(arr,arr1);
System.out.println(a);
}
}


package zifuchuan;

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
int [] arr = new int[6];
Arrays.fill(arr,1);
System.out.println(Arrays.toString(arr));//[1, 1, 1, 1, 1, 1]
}
}


08-01 60

02-15 239

03-16 441

02-07 90

07-21 5877

10-10 30

11-03 277

05-04 91

01-01 30

10-28 519

04-09 32

01-09 1521

08-03 99

11-03 2421

06-08 977

05-07 113

01-25 29

04-14 58万+

03-13 14万+

03-04 13万+

03-05 5705

03-08 4万+

03-08 7万+

03-10 12万+

03-12 11万+

03-13 11万+

03-14 9997

03-18 5426

03-18 8707

03-19 3万+

03-19 8万+

03-20 7470

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-29 21万+

03-29 1万+

03-29 10万+

03-30 16万+

05-28 5150

05-28 1万+

04-02 4万+

04-03 3073

04-06 7万+

04-07 5万+

04-09 8万+

04-09 2万+

05-17 7424

04-10 2474

04-11 3万+

04-15 5万+

04-18 4万+

04-20 4万+

04-24 3万+

04-29 5829

04-29 5670

04-30 4576

04-30 8896

04-30 8405

05-16 5万+

05-08 4万+

05-11 3万+

05-12 1万+

05-13 432

05-14 5652

05-14 1499

05-16 1万+

05-20 511

05-18 7646

05-27 555

05-18 2954

05-19 9819

05-21 5626

05-21 5636

05-25 3382

05-22 157

05-23 1万+

#### 63个Linux基础自测题,超过10个不会的,劝你还是别玩Linux了

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客