• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to compare two ArrayList with different size  RSS feed

 
Yama Kenji
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have 2 ArrayList but they are have different size
i try to do while loop but i'm not sure I do it right

pseudocode

ArrayList<String> listA
ArrayList<String> listB

int i = 0;

if(listA.size > listB.size) // to compare with size is larger
{
while(listA > 0)
{
if ListA equals to ListB

remove listA;
}
i++;
}

Any suggest for this thank you.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what you mean by these lines of pseudo-code:

Yama Kenji wrote: while(listA > 0)


But listA is a list of Strings, so what does it mean to ask whether that list is greater than zero? It doesn't even mean anything to ask whether an individual string is greater than zero.

if ListA equals to ListB


I don't know what you mean by asking whether the two lists are "equal". Since they have different lengths, I wouldn't call them equal anyway. Perhaps you meant to compare individual strings from the two lists? My guess is that you actually want to compare the individual strings from the two lists in some way, so if that's the case could you provide some more information about that?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yama Kenji wrote:So I have 2 ArrayList but they are have different size
i try to do while loop but i'm not sure I do it right

I'm afraid not. And your code suggests that you haven't really looked carefully at the List API - or maybe that you haven't dealt with Lists in your course yet.

However, the usual way of comparing Lists (or indeed any other sort of linear collection, like arrays) is as follows:
1. Compare elements from each List up to the length of the shorter one.
      If any of them are different, then the result of the comparison is the result of comparing those two elements.
2. If all common elements are the same, then the longer List is the "greater" one.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your pseudocode suggest you are not trying to compare Lists at all. It suggests you are trying to retain or remove some elements. You have not said what you are trying to do; you cannot work out how to do it until you have decided what to do.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!