There is a listmain multiple of 4. It is necessary to obtain from it unique listN lists of 4 elements and their number countlistN .

Lists of 4 items are formed according to certain rules

 listN(3)=listmain(0) listN(0)=listmain(1) listN(1)=listmain(2) listN(2)=listmain(3) listN(3)=listmain(4) listN(0)=listmain(5) listN(1)=listmain(6) listN(2)=listmain(7) ... listN(3)=listmain(i) listN(0)=listmain(i+1) listN(1)=listmain(i+2) listN(2)=listmain(i+3) 

Example

 listmain {1,2,3,4,5,6,7,8,1,2,3,4} 

should get

 list1 [2, 3, 4, 1] countlist1 = 2 list2 [6, 7, 8, 5] countlist1 = 1 

Came up with the following algorithm

  1. Original list is filled in ArrayList listmain
  2. Create the required number of lists of 4 elements and fill them in (so that you can make a set)
  3. listmain through the listmain by 4 elements and replace the values ​​in the created lists with 4 elements listmain and count their number

     int count1 = 0; int i = 0; int j = 0; for (i = 0; i < listmain.size(); i = i+4) { list1.set(j+3,listmain.get(i)); list1.set(j,listmain.get(i+1)); list1.set(j+1,listmain.get(i+2)); list1.set(j+2,listmain.get(i+3)); if ((list1.containsAll(listN))&&(listN.containsAll(list1))){ count1++; } } 

But there are more questions than answers.

  1. How to programmatically create an ArrayList ?

Those. I have to somehow write, create so many ( listmain.size())/4 new ArrayList with such names, so that they can be accessed later.

  1. How to compare among themselves all these ArrayList on 4 elements?

Create another ArrayList and place in it as ArrayList objects of 4 elements? And then?

  • Following your example, you should get from 1,2,3,4,5,6,7,8,1,2,3,4 4123 and 8567 , etc. not 2341 .... what is the error? ........ Как программно создать ArrayList - well, make a sheet of sheets) ......... - Alexey Shimansky
  • For this task, as a listN, in my opinion, a simple two-dimensional array of size Nx4 and the Arrays.equals method are Arrays.equals . - iksuy
  • 2341 - That's right. Here is the code to check - link - rezhisser
  • @rezhisser well, tell me) I gave you the answer ..... kind of explained that how ....... you can fix it yourself for listOfNums.add(list.get(i + 1)); listOfNums.add(list.get(i + 2)); listOfNums.add(list.get(i + 3)); listOfNums.add(list.get(i)); listOfNums.add(list.get(i + 1)); listOfNums.add(list.get(i + 2)); listOfNums.add(list.get(i + 3)); listOfNums.add(list.get(i)); ...... Link to the working code is - Alexey Shimansky

1 answer 1

In order to get "dynamically" several ArrayList not necessary to create them through new , it is enough just to have another sheet, with the type ArrayList<Integer> , ie:

 ArrayList<ArrayList<Integer>> listArr = new ArrayList<>(тут размер); 

total we will receive just "array" from ArrayList<Integer> in the size defined above:

 ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,1,2,3,4)); int listSize = list.size(); ArrayList<ArrayList<Integer>> listArr = new ArrayList<>(listSize/4); 

Further, completely completely freely fill these lists through a cycle:

 for (int i = 0; i < listSize; i += 4) { ArrayList<Integer> listOfNums = new ArrayList<>(); listOfNums.add(list.get(i + 1)); listOfNums.add(list.get(i + 2)); listOfNums.add(list.get(i + 3)); listOfNums.add(list.get(i)); listArr.add(listOfNums); } 

those. create a list, add numbers to it in the desired sequence, and then add this list to our "array" of lists.

As a result, the output will get

 [[2, 3, 4, 1], [6, 7, 8, 5], [2, 3, 4, 1]] 

And for counting the number of unique elements in the collection, the frequency method from the Collections class seems to be responsible. Those. if we give him an entrance

 list.add("Лилия"); list.add("Роза"); list.add("Лилия"); 

and write

 Collections.frequency(list, "Лилия"); 

then it will print the number 2 (via System.out.print )

In the end, you just need to use it:

 Set<ArrayList<Integer>> uniqueSet = new HashSet<ArrayList<Integer>>(listArr); for (ArrayList<Integer> temp : uniqueSet) { System.out.println("counts of " + temp + ": " + Collections.frequency(listArr, temp)); } 

will output:

 counts of [2, 3, 4, 1]: 2 counts of [6, 7, 8, 5]: 1 

HashSet - here to run in a loop only by unique values. After all, we know that Set is a set of non-repeating values.

Example entirely

 ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,1,2,3,4)); int listSize = list.size(); ArrayList<ArrayList<Integer>> listArr = new ArrayList<>(listSize/4); for (int i = 0; i < listSize; i += 4) { ArrayList<Integer> listOfNums = new ArrayList<>(); listOfNums.add(list.get(i + 3)); listOfNums.add(list.get(i)); listOfNums.add(list.get(i + 1)); listOfNums.add(list.get(i + 2)); listArr.add(listOfNums); } // Это чтоб пробежаться только по уникальным значениям листа. Set<ArrayList<Integer>> uniqueSet = new HashSet<ArrayList<Integer>>(listArr); for (ArrayList<Integer> temp : uniqueSet) { System.out.println("counts of " + temp + ": " + Collections.frequency(listArr, temp)); } 

An example on an external resource https://ideone.com/aNKfa2

  • Alex, thank you! - rezhisser
  • @rezhisser If you have been given the correct answer - vote for it \ check it correct (gray tick on the side of the answer). This will help others understand that the answer came up to solve the problem. - Alexey Shimansky