JAVA练手--数组

 

 

//数组
    public static void main(String[] args)  {
        //1. 数组排序和查找
        {
            int[] intA = {5, 4, 2, 3, 1};
            String[] StrA = {"ab", "cd", "ef"};
            
            Arrays.sort(intA);
            
            for(int a : intA){
                System.out.println("1  :"+a);
            }
            //采用的是二分法查找,所以必须要先排序
            int i = Arrays.binarySearch(intA, 3);
            System.out.println("1  :索引是从0开始,索引为"+i);
        }
        //2.  获取数组长度
        {
            int[] intA = {5, 4, 2, 3, 1};
            int[][] intB = new int[5][4];
            
            System.out.println("2  :"+intA.length);
            System.out.println("2  :"+intB.length+", "+intB[0].length);
        }
        //3. 数组反转(也可以仍为是集合的反转)
        {
            ArrayList<String> al = new ArrayList<String>();
            
            al.add("a");
            al.add("b");
            al.add("c");
            al.add("d");
            //采用集合的静态工具Collections进行反转
            Collections.reverse(al);
            Iterator it = al.iterator();
            while(it.hasNext()){
                System.out.println("3  :"+it.next());
            }
        }
        //4. 找到数组的最大值和最小值
        {
            Integer[] intA = {5, 4, 2, 3, 1};
            //数组转换成集合里的数组List
            //注意:这里的ArrayList并非java.util.ArrayList,而是Arrays类的静态内部类!
            Integer intB = Collections.max(Arrays.asList(intA));
            Integer intC = Collections.min(Arrays.asList(intA));
            System.out.println("4  :"+intB+", "+intC);
        }
        //5. 数组合并
        {
            Integer[] intA = {5, 4, 2, 3, 1};
            Integer[] intB = {8, 9, 7, 6};
            //现将数组intA转换为内部集合,然后在放入ArrayList中
            ArrayList al = new ArrayList(Arrays.asList(intA));
            al.addAll(Arrays.asList(intB));
            
            //第一种输出方式
            Iterator it = al.iterator();
            while(it.hasNext()){
                System.out.println("5  :"+it.next());
            }
            //第二种输出方式
            System.out.println("5  :"+al);
        }
        //6. 填充数组元素
        {
            int[] intA = new int[3];
            Arrays.fill(intA, 1);
            for(int a : intA)
                System.out.println("6  :"+a);
            
            System.out.println("6  --------");
            Arrays.fill(intA, 1, 3, 2);
            for(int a : intA)
                System.out.println("6  :"+a);
            
            System.out.println("6  --------");
            Integer[] intB = new Integer[3];
            Arrays.fill(intB, 100);
            for(Integer a : intB)
                System.out.println("6  :"+a);
            
        }
        //7. 数组扩容
        {
            int[] intA = {5, 4, 2, 3, 1};
            int[] intB = new int[7];
            intB[5] = 5;
            intB[6] = 6;
            System.arraycopy(intA, 0, intB, 0, intA.length);
            for(int a : intB)
                System.out.println("7  :"+a);
        }
        //8. 删除元素
        {
            ArrayList<Integer> al = new ArrayList<Integer>();
            al.add(10);
            al.add(20);
            al.add(30);
            al.remove(1);
            System.out.println("8  :"+al);
        }
        //9. 查看数组中是否包含某个元素
        {
            ArrayList<Integer> al = new ArrayList<Integer>();
            al.add(10);
            al.add(20);
            al.add(30);
            System.out.println("9  :"+al.contains(20));
            System.out.println("9  :"+al.contains(40));
        }
        //10. 查看数组是否相等
        {
            int[] intA = {1,2,3,4,5};
            int[] intB = {1,2,3,4};
            int[] intC = {1,2,3,4,5};
            Integer[] intD = {1,2,3,4,5};
            
            System.out.println("10 :"+Arrays.equals(intA, intB));
            System.out.println("10 :"+Arrays.equals(intA, intC));
            //System.out.println("10 :"+Arrays.equals(intA, intD));//报错
        }
    }

 

结果:

1  :1
1  :2
1  :3
1  :4
1  :5
1  :索引是从0开始,索引为2
2  :5
2  :5, 4
3  :d
3  :c
3  :b
3  :a
4  :5, 1
5  :5
5  :4
5  :2
5  :3
5  :1
5  :8
5  :9
5  :7
5  :6
5  :[5, 4, 2, 3, 1, 8, 9, 7, 6]
6  :1
6  :1
6  :1
6  --------
6  :1
6  :2
6  :2
6  --------
6  :100
6  :100
6  :100
7  :5
7  :4
7  :2
7  :3
7  :1
7  :5
7  :6
8  :[10, 30]
9  :true
9  :false
10 :false
10 :true

 

posted on 2018-03-28 09:17  maogefff  阅读(132)  评论(0编辑  收藏  举报

导航