0
static ArrayList random_ints=new ArrayList();
static ArrayList mean_storage=new ArrayList();
static ArrayList diff_means=new ArrayList();


random_ints = {834,438,234,124};
mean_storage = {867,459,254,189};

I have to find out the difference b/w these 2 array lists and store in "diff_means" array list

10
  • What did you try? Does list.retainAll() fits your need? Commented Mar 18, 2015 at 11:02
  • You mean to say your outcome would be like {33,21,20,65}? Commented Mar 18, 2015 at 11:04
  • @ArnaudDenoyelle I think the difference he is asking for is 834-867 or vice-versa !! Commented Mar 18, 2015 at 11:04
  • @NeerajJain So the OP basically needs a for loop??? Commented Mar 18, 2015 at 11:08
  • yeah my output like {33,21,20,65} Commented Mar 18, 2015 at 11:08

2 Answers 2

1

if your requirement is absolute difference between the list's element values then

Try This

int length=firstList.size();
for(int i=0;i<length;i++){
   resultList.add(Math.abs(firstList.get(i)-secondList.get(i)));
}

Assuming Size of Both the list is same !

Sign up to request clarification or add additional context in comments.

1 Comment

You mean resultList.add?
0

So unless you are looking for a specifically optimized solution, you can iterate the lists, get the numbers from both input lists at a particular index and find the difference (believing that mere subtraction would do, judging which number is greater!) and add it to third list. You may need to handle situation where size of one list is less than other.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.