I think I have the premise down correctly. I am sorting the 3 list inside of 1 for loop but I am getting array index and range errors. Why is that?
package com.codegym.task.task07.task0713;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Playing Javarella
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader bis = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
int s = Integer.parseInt(bis.readLine());
list.add(s);
}
ArrayList<Integer> list_THREE = new ArrayList<>();
ArrayList<Integer> list_TWO = new ArrayList<>();
ArrayList<Integer> list_OTHER = new ArrayList<>();
for (int x : list ){
if (x % 3 == 0 && x % 2 == 0) {
list_THREE.add(x);
list_TWO.add(x);
} else if (x % 2 == 0)
list_TWO.add(x);
else if (x % 3 == 0)
list_THREE.add(x);
else
list_OTHER.add(x);
}
printList(list_THREE);
printList(list_TWO);
printList(list_OTHER);
}
public static void printList(List<Integer> list) {
//write your code here
for(int y : list){
System.out.println(list.get(y));
}
}
}