There was a need to add 35 cars sheets (with a random number of instances) to the new listOfCars . Then do the sorting by the number of these copies.

 ArrayList<Car> cars = new ArrayList<Car>(); for (int i = 0; i < a; i++) { cars.add(new Car(a)); } ArrayList <ArrayList> listsOfCars = new ArrayList<ArrayList>(); for (int i = 0; i < 35; i++) { listsOfCars.add(cars); } Collections.sort(listsOfCars, SizeComparator); System.out.println(cars); System.out.println(listsOfCars); 

Comparator:

 public static Comparator<ArrayList> SizeComparator = new Comparator<ArrayList>() { @Override public int compare(ArrayList arrlist1, ArrayList arrlist2) { return arrlist1.size() - arrlist2.size(); } }; 

There is no problem in this code, since I brought him back to the look in which he works. But here I create one sheet of cars with a random number of instances, after which I add it 35 times to the listsOfCars list. But 35 different sheets with a random number of copies can not be added. Please indicate the right path.

  • There is no problem in this code, since I returned it to the view that works. But here I have one sheet of cars with a random number of instances, and then I add it 35 times to the listsOfCars list. But 35 different sheets with a random number of copies cannot be added. And the feeling that I am completely confused. - Oleg

4 answers 4

Create a new list ( cars ) on each of the 35 iterations with a random number ( count ) of elements in it:

 Random rand = new Random(); ArrayList<ArrayList<Car>> listsOfCars = new ArrayList<>(); for (int i = 0; i < 35; i++) { int count = rand.nextInt(100); ArrayList<Car> cars = new ArrayList<>(); for (int j = 0; j < count; j++) { cars.add(new Car(j)); } listsOfCars.add(cars); } Collections.sort(listsOfCars, SizeComparator); for (ArrayList<Car> cars : listsOfCars) { System.out.print(cars.size() + " "); } 

What are the restrictions on the size of the list - I do not know, I took "from 0 to 99 elements."

    I will give a simple example similar to yours:

     ArrayList<ArrayList<Integer>> arrayLists = new ArrayList<>(); Random random = new Random(); for(int i=0; i<10; i++) { int size = random.nextInt(10); arrayLists.add(new ArrayList<>(size)); for (int j=0; j<size; j++) { arrayLists.get(i).add(j); } } for (int i=0; i<arrayLists.size(); i++) { System.out.println(arrayLists.get(i).toString()); } Collections.sort(arrayLists, new Comparator<ArrayList<Integer>>() { @Override public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2) { return o1.size() - o2.size(); } }); System.out.println("---"); for (int i=0; i<arrayLists.size(); i++) { System.out.println(arrayLists.get(i).toString()); } 

    Here ArrayList<ArrayList<Integer>> is first created, then it is filled with random (by size) ArrayList<Integer> .

    Further, ArrayList<ArrayList<Integer>> is output to the console, sorted and displayed again.

    PS: Do so badly:

     ArrayList<ArrayList> listsOfCars = new ArrayList<ArrayList>(); 

    And so - well:

     ArrayList<ArrayList<Car>> listsOfCars = new ArrayList<>(); 

      You can like this:

       public static void main(String[] args) { int count = 35; List<List<Car>> listOfCarList = new ArrayList<>(count); for (int i = 0; i < count; i++) { Random random = new Random(); int size = random.nextInt(1_00); List<Car> cars = new ArrayList<>(size); for (int j = 0; j < size; j++) cars.add(new Car()); listOfCarList.add(cars); } listOfCarList .stream() .sorted((e1, e2) -> e1.size() - e2.size()) .forEach(System.out::println); } 

        Apparently this is what you need:

         ArrayList <ArrayList<Car>> listsOfCars = new ArrayList(); Random random = new Random(); for (int i = 0; i < 35; i++) { ArrayList<Car> cars = new ArrayList<Car>(); int randomInt = random.nextInt(max - min + 1) + min for (int j = 0; j < randomInt; j++){ cars.add(new Car(j)); } listsOfCars.add(cars); } 

        That is, you create a loop with 35 iterations and already in it create a sheet, generate a random number that will determine how many machines will be added to it. And already add this sheet to the parent.

        max and min respectively determine the boundaries of the random