• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

how to compare two ArrayList with different size

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 27469
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76888
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why should I lose weight? They make bigger overalls. And they sure don't make overalls for tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic