0
     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 ?

  • 1
    Sure this is your actual code? `B.add(A);` wouldn't compile - it should be `B.addAll(A);`. – Thomas Sep 15 '20 at 11:10
  • Your code won't compile. `B` is an `ArrayList` of `Double` type but you are trying to add another `ArrayList A` in to this `ArrayList`. You probably meant to add the elements of `A` in `B`. You also have a typo in the last line of code: `System.out.Println` – Yousaf Sep 15 '20 at 11:12
  • "Please What is my Problem ?" - have a look at what `==` means for objects (and why you should use `equals()` instead). You should also learn to step through your code with a debugger and have a look at those `if-if-if...` sequences. Finally, your code looks _very_ odd and fragile, what are you trying to achieve? – Thomas Sep 15 '20 at 11:12
  • when i run your code (with little fix like B.addAll(A), or println without capital P ) i have the following result : 2nd 1st 5th 4th 3rd – CLAIN Cyril Sep 15 '20 at 11:16
  • _I run the Code and gives me this output [1st , 2nd , 3rd , 4th , 5th]_ - your code won't compile but if you fix the errors, your code will not produce the output that you are claiming that it produces. – Yousaf Sep 15 '20 at 11:17
  • Yes I want add the Element of A in B – Najibu Tanimu Sep 15 '20 at 12:49
  • So Now how can I achieve the this output [ 1st , 1st , 3rd , 4th , 4th ] in my code – Najibu Tanimu Sep 15 '20 at 12:53

1 Answers1

0

The == operator compares the references. You should use the .equals() function instead to compare the values.

for example, A.get(a).equals(B.get(4))

see this answer for details.

Mubin
  • 130
  • 1
  • 9