• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Greater than between the elements of two Integer Arraylist

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ok so I have two integer arraylist  and I want to compare each element that matches  the index of the other arraylist. I then want to loop through both arraylist and if one element is greater than the other I want to store that element in one of two new list.              


The Arraylist

arr1 ={3,4,0};   arr2={7,2,1};


Result I want

arr1greater {4};  arr2greater {7,1};


how can i do this?

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is probably easy to do. In fact you have explained it yourself because your post is clear and tells us exactly what you want to do. You can iterate the two Lists and see which element at each index is larger. What are you going to do if the two values are the same? Are you sure the two input Lists will have the same size? Note that Ar‍rayList implements RandomAccess; read that link because it gives hints about the quickest way to iterate your Lists.
Don't call a List arrXXX because you will confuse yourself about whether you are dealing with a List or an array.
 
Sean Reliford
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the elements are the same than return nothing or null. and yes the list will always be the same size. I can use a for loop. I just don't know the logic of getting the element I want when I want it.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using List's then you can use
to get a specific entry.
 
Sean Reliford
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can no one help me??? this is seriously frustrating.
 
Ranch Hand
Posts: 491
23
Eclipse IDE Firefox Browser Spring VI Editor AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Reliford wrote:Can no one help me??? this is seriously frustrating.


Hi sean,
It is frustrating because you are not thinking about the problem with ease,i mean the way you deal it if it's really practical.let me exemplify this-
suppose i have 2 buckets each having 3 balls having a number marked on each ball.now i want to compare the balls at the respected positions and then removes the one having smaller number marked on it and in case if the number clash then i will throw the both ones.
now the steps involved are:

1) look in both the baskets.
2) get both the balls at the same position.
3) compare them.
4) throw the least one or both if equal.
5)repeat the procedure for 3 times.

now the java analog for this procedure is
1)iteration
2)List get(int index) method or Iterator's next() method.
3)use >,<,==(or like for fractions use Double.compare(double,double) or whichever is applicable).
4)since you are removing the element from least,use Iterator's remove() method.
5)you can use Iterator's hasNext() for this part.
you can use list.iterator() for getting a Iterator over the list....now you got a big hint rest is your job.

for the next time i will advice you to think about the programming problems practically(that's for what it's discovered),if you didn't than probably it's not for you.

Kind Regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sean Reliford wrote:. . . I just don't know the logic of getting the element I want when I want it.

There was an example in the Random Access link I posted yesterday.
You have already explained what you want to do (though not why somebody wants such a List of differences). You said you wanted to go through the Lists and do something if the element in List 1 is greater than that in List 2. That explanation is straightforward and well‑thought‑out, so you shou‍ld find it easy to turn into code. I presume you don't mean return null if the two elements are the “same”; I think you mean to do nothing.

PK: I think you are overthinking the problem. It doesn't say anything about removal, and I think that Iterators would complicate matters. I think OP did think about the problem and then got cold feet not knowing how to obtain the elements.
 
reply
    Bookmark Topic Watch Topic
  • New Topic