Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

The right collection

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sameera Abeysinghe
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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/
 
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic