Anitha Tadekoru
Anitha Tadekoru

Reputation: 1

How to find the difference between two arraylist values in java?

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

Upvotes: 0

Views: 1170

Answers (2)

Amit K
Amit K

Reputation: 51

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.

Upvotes: 0

Neeraj Jain
Neeraj Jain

Reputation: 7730

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 !

Upvotes: 1

Related Questions