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
- Original list is filled in
ArrayListlistmain - Create the required number of lists of 4 elements and fill them in (so that you can make a set)
listmainthrough thelistmainby 4 elements and replace the values in the created lists with 4 elementslistmainand count their numberint 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.
- 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.
- How to compare among themselves all these
ArrayListon 4 elements?
Create another ArrayList and place in it as ArrayList objects of 4 elements? And then?
1,2,3,4,5,6,7,8,1,2,3,44123and8567, etc. not2341.... what is the error? ........Как программно создать ArrayList- well, make a sheet of sheets) ......... - Alexey ShimanskyArrays.equalsmethod areArrays.equals. - iksuylistOfNums.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