• Post Reply Bookmark Topic Watch Topic
  • New Topic

Objects in Arraylist  RSS feed

 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody!

I want to make function witch will crate an object and put him in Arraylist, so I could later acces to those object, so can I do this



but, would I loose acces to objects in Arraylist when my referece (o) is associated with new Object? Remeber,I want to have n same object with diferent values in Arraylist.

kind regardes
 
Syamsul Hussin
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you could use get method to obtain the reference again

 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but if I assign on some object his name with setName("John"), and latter I want to delete this object from the list, could I do this

 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi cowboys,

in your methods everything is local. You cannot access any of your variables from outside, so whatever has been done in your methods evaporates after they have been called.

Perhaps use the list as a member of your class. And also you should have the object to be added, deleted or whatsoever as a paramater in the signature of your methods.


Yours,
Bu.
 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a List in my class, and I get to her by getList(), and how could I tell the function with parameter witch object I want delete cose I must search for him first by getName("John")?

regards
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marco,

you could pass a String to you method as:




Otherwise, the function only deletes Johns.

By the way, I just did it for explanation, but you should not provide pseudocode here. If you do, nobody will know really what your problem is.
Your code for example can never compile, as Object simply does not have a method get().


Yours,
Bu.
 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again,

You ment something like this:



would I now delete object with specific name from the list?

Thnx
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method has a few bugs.
Finally in Java the convention is to begin method names with lower case letters, so your method would be called deleteObject(). This convention is obviously not enforced by the compiler, but it does make the intention of your code clearer for others reading it.
[ October 15, 2006: Message edited by: Garrett Rowe ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also read up on Maps. They store and retrieve objects by keys. They key can be any other object but is often a string. If you always fetch things by the same field, in this case the field is name, a Map will do the job nicely.
 
Marko Debac
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi to all!

..or I can do this

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!