• Post Reply Bookmark Topic Watch Topic
  • New Topic

comparing object in ArrayList  RSS feed

 
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say I have an ArrayList that contains SomeClass1 objects that hold two/three String values.

ArrayList name "addItToList" now contains three elements;
index 0 -> "AAA" "BBB"
index 1 -> "AAA" "BBB"
index 2 -> "AAA" "BBB" "CCC"

Is it possible to compare the index 0 and index 1, if both of them have same String value?

For example,
if
index 0 -> "AAA" "BBB"
index 1 -> "AAA" "BBB"
then
return true

if
index 0 -> "AAA" "BBB"
index 1 -> "BBB" "AAA"
then
return true

Do I need to make ArrayList of String Array?
Is it possible without using multi-dimensional array?

I made this code for the question purpose.
 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch
Your problem can be solved using comparator.
this is a link to comparator

Go through it and suggest here if you got any idea.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There ain't no such thing as a multi‑dimensional array; what appears to be multi‑dimensional is in fact an array of arrays, which is better. I think you shou‍ld avoid proliferating arrays; you can tie yourself in knots with multiple arrays.
I think you need to find out about the equals() method. Start by searching this forum because somebody was asking about equals only a few days ago. Let's see if I can find anything. Got it. NB: There is another thread referred to in that discussion: if you need to look at it, here it is. Beware: it isn't easy.
I get the impression that you want to define the equals() method in your class such that
"AAAA", "BBBB" equals
"BBBB", "AAAA"
Then you can simply test whether the first element in your List equals the second. YOu need a pencil and paper and (most important) an eraser, so you can work out some sort of definition of equality there. Hint: Since String already has a correctly&#s2011;written equals method, you can be confident about using "AAAA".equals("AAAA").
 
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again!!
why did you define the SomeClass2.I cannot see anything special in it,it is just used in adding instances of SomeClass1 to the List(defined in SomeClass2) but that can be done also in SomeClass1 by defining a List type instance variable in it.
Another issue-your addList is a instance method.so as per its implementation if you are required to add instance SomeClass1("Hi","Nico") to the List then you have to do somewhat like and it will redundantly create the another instance(copy) inside the method.in fact implementation inside the addList should be like "data1.makeList(this)".
you can do nothing with the list defined in SomeClass2 as it is not accessible in SomeClass1.also you have not preserve the local variable of type SomeClass2 (defined in addList) any where in class SomeClass1 so after the method addList will return the local variable will get eligible for Garbage Collection.
try to use type List for a reference variable(program to interface)
you have done a very bad selection of names for the methods and variables.use noun for variables and verb for methods.see the name of list you have used,"addItToList",it seems like it is doing some work regarding adding something to list,though in actual it is just a name.

so i think you should first correct these things.

Praveen.
 
nick Mercado
Greenhorn
Posts: 12
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Tapas, Campbell & praveen!

I used equal() method and getter to compare two String object.

something like this


It took me couple hours to figure this out but I think I learned a lot from this!


 
Campbell Ritchie
Marshal
Posts: 56610
172
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!