Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes List -in  collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List -in  collections" Watch "List -in  collections" New topic
Author

List -in collections

Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
i have just started with Collections concepts.
I wrote a program using List-arrayList.
What i saw ,was if i add the same object twice to the ArrayList ,using
add(index,obj) or add(obj)
It doesnt increment but gives same index of previous similar one.
Duplicates are possible but index remains same.

Is that so .& why

Thnx
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
How are you getting the index of the object you added? You could use list.indexOf() or list.lastIndexOf() and get different results.

Keep in mind that an ArrayList is a list of references, not a list of objects. If you add the same object twice, your list will contain two references to the same object at different indexes.

For example:



If you still don't understand, post your code so we can see what's going on.
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
This is my code ,
i try to add dog1 object at different indexs or without mentioning index
with add(inx,obj) & add(obj )
but when i get it displayed ,it shows same index as prev duplicate was given .

_______________________________________________________________
import java.util.*;

public class ListDemo1 {

public static void main(String[] args) {
List list1 = new ArrayList();
ArrayList list2 = new ArrayList();
Object dog1 = "dalmatian";
Object dog2 = "spaniel";
Object dog3 = "shephard";
list1.add(0, dog1);
list1.add(1, dog1);
list1.add(2, "dalmatian");
list1.add( new String("pointer"));
System.out.println("size is " + list1.size());
System.out.println("Last Indx of dog3 " + list1.lastIndexOf(dog2));

for (int i = 0; i < list1.size(); i++) {
System.out
.println(list1.indexOf(list1.get(i)) + " " + list1.get(i));
}
}}
____________________________________________


My output is ::

size is 4
Last Indx of dog3 -1
0 dalmatian
0 dalmatian
0 dalmatian
3 pointer
__________________________________________
Leandro Melo
Ranch Hand

Joined: Mar 27, 2004
Posts: 401
Hi.

That's because indexOf returns the index of the first occurence the object supplied. Since in the first three positions you have the same string "dalmatian", indexOf always returns 0. Take a look at the Java API for ArrayList. Did you get it?


Leandro Melo
SCJP 1.4, SCWCD 1.4
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
ok,so actual index may be different from indexOf() returned value.

With remove() ,does index move up ,as opposiite with add().
&
How do we know ,the real index values,lastIndexOf() gives last occurance.

Thnx a lot for clearing my doubt.
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
ok,
i got it .so actual index may be different from indexOf() returned value with duplicates.

With remove() ,does index move up ,as opposiite with add().
&
How do we know ,the real index values,lastIndexOf() gives last occurance.

Thnx a lot for clearing my doubt.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List -in collections
 
Similar Threads
Split String to ArrayList
URL string replacing
how to sort ArrayList by multiple values?
Alternative to Hashtable