ArrayList<Double> A = new ArrayList<Double>();
ArrayList<Double> B = new ArrayList<Double>();
ArrayList<String> C = new ArrayList<String>();
A.add(8.0);
A.add(8.0);
A.add(4.1);
A.add(4.1);
A.add(7.0);
B.addAll(A);
Collections.sort(B)
After Sorting the ArrayList B is Rearranged like this [4.1 , 4.1 , 7.0 , 8.0 , 8.0] Now to add element in the ArrayList C I make a loop below
for (int a = 0 ; a < 5 ; a++ ) {
if( A.get(a) == B.get(4) ) {
C.add(a , "1st");
}
if( A.get(a) == B.get(3) && A.get(a) != B.get(4) ) {
C.add(a , "2nd");
}
if( A.get(a) == B.get(2) && A.get(a) != B.get(3) ) {
C.add(a , "3rd");
}
if( A.get(a) == B.get(1) && A.get(a) != B.get(2) ) {
C.add(a , "4th");
}
if( A.get(a) == B.get(0) && A.get(a) != B.get(1) ) {
C.add(a , "5th");
}
System.out.println(C.get(a));
}
I run the Code it gives Error but with different Values of A which are not equal like
A.add(2.3);
A.add(3.1);
A.add(4.8);
A.add(0.8);
A.add(2.5);
The loop works clearly and gives this output[ 1st , 2nd , 3rd , 4th , 5th]
I want achieve this output this output[1st , 1st , 3rd , 4th , 4th ]
if ArrayList A contain some elements which are the same like the First
added Elements in A.
please how todo so ?