当前位置:   article > 正文

集合的嵌套遍历

集合的嵌套遍历

嵌套集合

集合中嵌套集合,并遍历。

public class ArrayListDemo {
    public static void main(String[] argv){
        ArrayList<ArrayList<Stu>> list = new ArrayList<ArrayList<Stu>>();

        ArrayList<Stu> firstArrayList = new ArrayList<Stu>();
        firstArrayList.add(new Stu(19,"lili"));
        firstArrayList.add(new Stu(18,"jack"));

        ArrayList<Stu> secondArrayList = new ArrayList<Stu>();
        secondArrayList.add(new Stu(20,"约翰"));
        secondArrayList.add(new Stu(22,"陈无"));

        list.add(firstArrayList);
        list.add(secondArrayList);

        for(ArrayList<Stu> al :list){
            for(Stu s:al){
                System.out.println(s);
            }
        }
    }
}

输出:
Stu{age=19, name='lili'}
Stu{age=18, name='jack'}
Stu{age=20, name='约翰'}
Stu{age=22, name='陈无'}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

获取10个1-20之间的随机数,要求不能重复

public class RandomDemo {
    public static void main(String[] args) {
        // 创建产生随机数的对象
        Random r = new Random();

        // 创建一个存储随机数的集合。
        ArrayList<Integer> array = new ArrayList<Integer>();

        // 定义一个统计变量。从0开始。
        int count = 0;

        // 判断统计遍历是否小于10
        while (count < 10) {
            //先产生一个随机数
            int number = r.nextInt(20) + 1;

            //判断该随机数在集合中是否存在。
            if(!array.contains(number)){
                //如果不存在:就添加,统计变量++。
                array.add(number);
                count++;
            }
        }

        //遍历集合
        for(Integer i : array){
            System.out.println(i);
        }
    }
}

输出:
10
19
3
18
7
15
11
6
9
12

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

集合转数组 toArray

  • public Object[] toArray() //以正确的顺序(从第一个到最后一个元素)返回一个包含此列表中所有元素的数组。
  • public <T> T[] toArray(T[] a) //返回的数组的运行时类型是指定数组的运行时类型

给定集合,排序

public class ArrayListDemo{
    public static void main(String[] args) {
        // 创建产生随机数的对象
        Random r = new Random();

        // 创建一个存储随机数的集合。
        ArrayList<Integer> array = new ArrayList<Integer>();
        array.add(4);
        array.add(1);
        array.add(2);
        array.add(8);
        array.add(7);
             
        // 把集合转成数组
        Integer[] objArr = new Integer[array.size()];
        array.toArray(objArr);
     
        // 对数组排序
        Arrays.sort(objArr);

        //遍历集合
        for(Integer i : objArr){
            System.out.println(i);
        }
        System.out.println("---------------");
    }
}

输出:
1
2
4
7
8

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

数组转集合

public static List asList(T… a): 把数组转成集合

public class ArraysDemo {
    public static void main(String[] args) {
        // 定义一个数组
        String[] strArray = { "hello", "world", "java" };
        List<String> list1 = Arrays.asList(strArray);
        list1.set(1, "javaee1");
        // java.lang.UnsupportedOperationException
        // list1.add("javaee");

        List<String> list2 = Arrays.asList("hello", "world", "java");
        list2.set(1, "javaee2");
        // java.lang.UnsupportedOperationException
        // list2.add("javaee");

        for (String s : list1) {
            System.out.println(s);
        }
        System.out.println("---------");

        for (String s : list2) {
            System.out.println(s);
        }
    }
}

输出:
hello
javaee1
java
---------
hello
javaee2
java

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

注意:虽然可以把数组转成集合,但是集合的长度不能改变

可变参数

可变参数:定义方法的时候不知道该定义多少个参数
格式:修饰符 返回值类型 方法名(数据类型… 变量名) {}

public class ArgsDemo {
    public static int sum(int... a) {
        int s = 0;
        for(int x : a){
            s += x;
        }
        return s;
    }

    public static void main(String[] args) {
        // 2个数据求和
        int a = 10;
        int b = 20;
        int result = sum(a, b);
        System.out.println("result:" + result);

        // 3个数据的求和
        int c = 30;
        result = sum(a, b, c);
        System.out.println("result:" + result);

        // 4个数据的求和
        int d = 30;
        result = sum(a, b, c, d);
        System.out.println("result:" + result);

        // 需求:我要写一个求和的功能,到底是几个数据求和呢,我不太清楚,但是我知道在调用的时候我肯定就知道了
        // 为了解决这个问题,Java就提供了一个东西:可变参数
        result = sum(a, b, c, d, 40);
        System.out.println("result:" + result);

        result = sum(a, b, c, d, 40, 50);
        System.out.println("result:" + result);
    }
}

输出:
result:30
result:60
result:90
result:130
result:180

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

注意:可变参数其实是一个数组,如果一个方法有可变参数,并且有多个参数,那么,可变参数肯定是最后一个

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/489054
推荐阅读
相关标签
  

闽ICP备14008679号