I am trying to find the combination of same numbers in an array in java e.g i have number in an array e.g numbers{1,2,2,2,1,1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1} .So i want the program to check all combinations of number that occurred in a sequence of order only like 1, or 2,2,2 or 1,1,1 or 2,2,2 then it will check how many times did 2 or 1 occur so the occurred number will be stored in an arraylist. In that case first 1 occurred 1 time then 2 occured 3 times then 1 occurred 3 times then 2 occurred 3 times and so on it will show how elements are arranged in an array .i have wrote a code but it is not working in java as i want it to
public class Main {
private static ArrayListPlistItem=new ArrayList();
private static ArrayListNlistItem=new ArrayList();
private static ArrayListPwillhappen=new ArrayList();
private static ArrayListNNhappen=new ArrayList();
private static ArrayList LhappenList=new ArrayList();
private static ArrayList NhappenList=new ArrayList();
private static ArrayList LhappenListTotal=new ArrayList();
private static ArrayList NhappenListTotal=new ArrayList();
private static int Lhappen=0;
private static int Lhappen2=0;
private static int Nhappen=0;
private static int Nhappen2=0;
private static int () items ={1,2,2,2,1,1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1};
private static int () Nhappenitems =new int(1);
private static int () Lhappenitems =new int(1);
public static void main(String ()args){
PlistItem.add(1);
NlistItem.add(2);
//comparing elements from an array which with an arraylist number
for(int y=0; y<items.length; y++){
if(PlistItem.get(0)==items(y)){
//counter to count how many times 1 occurred
Lhappen++;
//counter to count how many times 1 occurred
Lhappen2++;
//adding the counter in arraylist
Pwillhappen.add(0, Lhappen);
//adding the numbers how many times it occured in arraylist
Lhappenitems(0)=Lhappen2;
//adding in total number it occur in an arraylist
LhappenListTotal.add(0,Pwillhappen.get(0));
//setting the counter into 0
Nhappen=0;
}else if(NlistItem.get(0)==items(y)){
//counter to count how many times 2 occurred
Nhappen++;
//counter to count how many times 2 occurred
Nhappen2++;
//adding the counter in arraylist
NNhappen.add(0,Nhappen);
//adding the numbers how it many times it occurred in arraylist
Nhappenitems(0)=Nhappen2;
//adding in total number it occur in an arraylist
NhappenListTotal.add(0, NNhappen.get(0));
//setting the counter into 0
Lhappen=0;
}
}
System.out.println(” RESULTS ” );
System.out.println(“——————————————————————————–“);
System.out.println(“1 occurrence total “+ Nhappenitems(0) +” times”);
System.out.println(“2 occurence total “+ Lhappenitems(0) +” times”);
System.out.println(“——————————————————————————–“);
for(int i=0; i<LhappenListTotal.size(); i++){
System.out.println(” combination of 1’s occurred “+ LhappenListTotal.get(i) +” times”);
}
System.out.println(“——————————————————————————–“);
for(int i=0; i<NhappenListTotal.size(); i++){
System.out.println(“combination of 2’s occurred “+ NhappenListTotal.get(i)+” times”);
}
}
}
RESULTS
1 occurrence total 11 times
2 occurence total 10 times
combination of 1’s occurred 1 times
combination of 1’s occurred 3 times
combination of 1’s occurred 2 times
combination of 1’s occurred 1 times
combination of 1’s occurred 2 times
combination of 1’s occurred 1 times
combination of 1’s occurred 3 times
combination of 1’s occurred 2 times
combination of 1’s occurred 1 times
combination of 1’s occurred 1 times
combination of 2’s occurred 2 times
combination of 2’s occurred 1 times
combination of 2’s occurred 3 times
combination of 2’s occurred 2 times
combination of 2’s occurred 1 times
combination of 2’s occurred 3 times
combination of 2’s occurred 2 times
combination of 2’s occurred 1 times
combination of 2’s occurred 3 times
combination of 2’s occurred 2 times
combination of 2’s occurred 1 times
my code when i run it is showIng as above results instead i want my results to be as below so help me to solve the code to produce the results as below it show produce the result same as the elements how they are arranged in an array.. items ={1,2,2,2,1,1,1,2,2,2,1,1,2,2,2,1,1,1,2,2,1};
RESULT
combination of 1’s occurred 1 times
combination of 1’s occurred 3 times
combination of 1’s occurred 2 times
combination of 1’s occurred 3 times
combination of 1’s occurred 1 times
combination of 2’s occurred 3 times
combination of 2’s occurred 3 times
combination of 2’s occurred 3 times
combination of 2’s occurred 2 times