jQuery in Action, 2nd edition
The moose likes Java in General and the fly likes The right collection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "The right collection " Watch "The right collection " New topic
Author

The right collection

Sameera Abeysinghe
Ranch Hand

Joined: Nov 15, 2004
Posts: 104
Hi all,

I need an array to put Car objects
Car is a class that extends JPanel

This array will be accessed severe times by some class�s
So searching is involved
The size of the array in unknown. So must be growable.
And finally 10 entirely separate threads will access this array.
This 10 threads will be started like
t1.start();
t2.start();
��..
��..
t10.start();
t1 to t10 is thread objects
So we don�t know what thread will access the array first or what time or���.
So synchronizes in needed

My problem is without implementing an array to suet this features what collection I can use

Thank you
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
On which criteria will your searches be based?

As an aside, having a Car class inherit from JPanel sounds like a very questionable desing to me.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Sameera Abeysinghe
Ranch Hand

Joined: Nov 15, 2004
Posts: 104
One by one
One after another
Will not be like giving a index and access in the middle
Will only be accessed one after another
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
sounds like a perfect situation to use a Vector, which is a synchronized, growable arraylist.

you might want to check the java-doc, or have a look at the collection part of sun's java tutorial.

many greetings,
jan
Sameera Abeysinghe
Ranch Hand

Joined: Nov 15, 2004
Posts: 104
Ok so I create a vector object with
initialCapacity= 10 and capacityIncrement = 1
I use vector.add() to add elements
How can I access the elemnt?
By vector.get()
Will it remove the element from that vector???

And finals when I use any methods in the vector since it is synchronize I don�t have to bother about putting notify of notifyall or synchronize or what ever

Is it???

How about if I use vector.toArray() in each of my 10 threads and the do this operation using that object array
Is it ok ???


Thanks for your fast replys
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Don't use a Vector - this is a red herring.
http://jqa.tmorris.net/GetQAndA.action?qids=53&showAnswers=true

You likely want a java.util.ArrayList.
http://java.sun.com/docs/books/tutorial/collections/


Tony Morris
Java Q&A (FAQ, Trivia)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Originally posted by Sameera Abeysinghe:
Ok so I create a vector object with
initialCapacity= 10 and capacityIncrement = 1
I use vector.add() to add elements
How can I access the elemnt?
By vector.get()
Will it remove the element from that vector???

No. You must use remove(int i) for that. get(int i) only returns the element without removing.
And finals when I use any methods in the vector since it is synchronize I don’t have to bother about putting notify of notifyall or synchronize or what ever

Is it???

Well if only the access to the List (yes with capital L - you should declare it as a List so you can switch implementations easily) then you don't need any other synchronization.
If you want to remove items but wait when the List is empty until some other Thread fills it then you need additional wait / notify / notifyAll statements.
How about if I use vector.toArray() in each of my 10 threads and the do this operation using that object array
Is it ok ???

Only if none of the Threads need to modify the List. Otherwise the array will be a snapshot, and not get updated.

Originally posted by Tony Morris:
Don't use a Vector - this is a red herring.
http://jqa.tmorris.net/GetQAndA.action?qids=53&showAnswers=true

You likely want a java.util.ArrayList.
http://java.sun.com/docs/books/tutorial/collections/

I disagree. ArrayList is the way to go if you don't need any synchronization. If you do, there are only two options:
1) Vector
2) call Collections.synchronizedList(List l) with an ArrayList

I don't see any advantages to using the second option - you create another wrapper object around your existing List, and you still get synchronization - something that is done using Vector immediately.
[ November 01, 2005: Message edited by: Rob Spoor ]

SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780


I disagree. ArrayList is the way to go if you don't need any synchronization. If you do, there are only two options:
1) Vector
2) call Collections.synchronizedList(List l) with an ArrayList

3) CopyOnWriteArrayList


There is no emoticon for what I am feeling!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Rob Spoor:

I disagree. ArrayList is the way to go if you don't need any synchronization. If you do, there are only two options:
1) Vector
2) call Collections.synchronizedList(List l) with an ArrayList


Keep in mind that using a synchronized collection doesn't really guarantee thread safety. Often you will need to synchronize a sequence of actions, when one call on the collection relies on the result of a prior call.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The right collection
 
Similar Threads
Output even more unexpected than I was expecting!
adding more threads
Clarify "at line #" on exam
Regarding Synchronization
Doubt in Threads Synchronization