java数组知识点总结

2 数组

2.1 声明&遍历数组&数组长度

public class ArrayTest {

    public static void main(String[] args) {

//     int[] a=new int[5]; //声明一个数组

       int[] a={1,2,3,4,5};

       for (int i= 0; i < a.length; i++) {

           System.out.println(a[i]);// 1 2 3 4 5

       }

       System.out.println("数组长度为:"+a.length);  // 5

    }

}

2.2 小Test

/*

 * 找出数组中的最大值

 */

public class ArrayTest {

    public static void main(String[] args) {

       int[] a={1,11,111,1111};

       int max=a[0];

       for (int i = 0; i < a.length; i++) {

           if(a[i]>max){

              max=a[i];

           }

       }

       System.out.println("数组最大值为:"+max); 

    }

}

2.3 数组反转

 

/*

 * 数组反转

 */

public class ArrayTest {

    public static void main(String[] args) {

       int[] a={1,10,100,1000};

       for (int i = 0; i < a.length; i++) {

           System.out.println("原数组是:"+a[i]);

       }

       reverse(a);

       for (int i = 0; i < a.length; i++) {

           System.out.println("反转后的数组是:"+a[i]);

       }

    }

    private static void reverse(int[] a) {

       for (int i = 0; i < a.length/2; i++) {

           int temp;

           temp=a[i];

           a[i]=a[a.length-1-i];

           a[a.length-1-i]=temp;

       }

    }

}

2.4 增强for循环遍历数组

 

/*

 * 数组增强for循环遍历

 */

public class ArrayTest {

    public static void main(String[] args) {

       int[] a = { 1, 10, 100, 1000 };

       for (int i : a) {

           System.out.println(i);

       }

    }

}

2.5 复制数组

/*

 * 复制数组

 */

public class ArrayTest {

    public static void main(String[] args) {

        int a [] = {1,10,15,20,35,80};

        int b[] = new int[4];//分配了长度是4的空间,但是没有赋值

        // System.arraycopy(src, srcPos, dest, destPos, length)

        //src: 源数组

        //srcPos: 从源数组复制数据的启始位置

        //dest: 目标数组

        //destPos: 复制到目标数组的启始位置

        //length: 复制的长度      

        System.arraycopy(a, 0, b, 0, 4);

        for (int i = 0; i < b.length; i++) {

            System.out.print(b[i] + " ");

        }

    }

}

2.6 二维数组

/*

 * 二维数组的遍历

 */

public class ArrayTest {

    public static void main(String[] args) {

       int a[][] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

       for (int i = 0; i < a.length; i++) {

           for (int j = 0; j < a[i].length; j++) {

              System.out.print(a[i][j] + " ");

           }

       }

    }

}

2.7 数组复制(Array类)

/*

 * 复制数组(Arrays)

 */

public class ArrayTest {

    public static void main(String[] args) {

        int a[] = new int[] { 10, 60, 65, 81, 35, 8 };

        // copyOfRange(int[] original, int from, int to) 包含头不包含尾

        // 第一个参数表示源数组

        // 第二个参数表示开始位置(取得到)

        // 第三个参数表示结束位置(取不到)

        int[] b = Arrays.copyOfRange(a, 0, 3);//index 从0开始的

        for (int i = 0; i < b.length; i++) {

            System.out.print(b[i] + " ");   // 10 60 65

        }

    }

}

2.8 将数组转换为字符串

public class ArrayTest {

    public static void main(String[] args) {

        int a[] = new int[] { 10, 60, 65, 81, 35, 8 };

        String array=Arrays.toString(a);

        System.out.println(array);// [10, 60, 65, 81, 35, 8]

    }

}

2.9 数组排序

 

public class ArrayTest {

    public static void main(String[] args) {

        int a[] = new int[] { 10, 60, 65, 81, 35, 8 };

        System.out.println(Arrays.toString(a));// [10, 60, 65, 81, 35, 8]

        //进行sort排序

        Arrays.sort(a);

        // sort排序后[8, 10, 35, 60, 65, 81]

        System.out.println("sort排序后:"+Arrays.toString(a));

    }

}

2.10 查找

使用binarySearch进行查找之前,必须使用sort进行排序
如果数组中有多个相同的元素,查找结果是不确定的

public class ArrayTest {

    public static void main(String[] args) {

        int a[] = new int[] { 10, 60, 65, 81, 35, 8 };

        //进行sort排序

        Arrays.sort(a);

        System.out.println(Arrays.toString(a)); // [8, 10, 35, 60, 65, 81]

        // index是进过sort排序后的位置

        System.out.println("数组中 60 出现的位置:"+Arrays.binarySearch(a, 60));

    }

}

2.11比较数组是否相同

public class ArrayTest {

    public static void main(String[] args) {

        int a[] = new int[] { 10, 60, 65, 81, 35, 8 };

        int b[] = new int[] { 10, 60, 65, 81, 35, 10 };

        System.out.println(Arrays.equals(a, b)); // false

    }

}

2.12 数组填充

public class ArrayTest {

    public static void main(String[] args) {

        int a[] = new int[6];

        Arrays.fill(a, 6);

        System.out.println(Arrays.toString(a)); // [6, 6, 6, 6, 6, 6]

    }

}

猜你喜欢

转载自blog.csdn.net/yloveyzcl/article/details/81452707
今日推荐