Using operator[] with an ArrayList

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2004
Posts: 24
Reputation: ray_broome is an unknown quantity at this point 
Solved Threads: 0
ray_broome ray_broome is offline Offline
Newbie Poster

Using operator[] with an ArrayList

 
0
  #1
Mar 3rd, 2005
Hi,

I am writing a java application where i am trying to create an arraylist of "Item" objects. An Item has certain methods such as getStartValue(), etc.

What i am trying to do is to access an Item within my arraylist using operator[] and then call a method for that object
e.g:
  1. ArrayList itemsList = new ArrayList();
  2. ...
  3. itemsList.add(anItem);
  4. double amount = itemsList[0].getStartValue();

I looked through the documentation but i did not see anything related. There was also nothing related in the Vector documentation but i think this is possible using a Vector.
Any ideas or suggestions?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: Using operator[] with an ArrayList

 
0
  #2
Mar 3rd, 2005
I'm not sure if the array list would be the best way to go, but I could be wrong..Have you tried just creating an array of the Item Objects? But of course then you couldn't add extra items at runtime.

By the way, is this getStartValue() method going to return different results for each Item?

I mean, would each of these return different values:

double amount = itemsList[0].getStartValue();
double amount2 = itemsList[1].getStartValue();
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Using operator[] with an ArrayList

 
0
  #3
Mar 4th, 2005
An ArrayList isn't an array. It (nor Vector which you shouldn't use, it's there for legacy use only) has different mechanisms.

To retrieve an item from your List use the get(index) method.

Under Java 1.5 (5.0, Tiger) you should thus do the following:

  1. ArrayList<Item> itemsList = new ArrayList<Item>();
  2. ...
  3. Item anItem = new Item();
  4. ...
  5. itemsList.add(anItem);
  6. double amount = itemsList.get(0).getStartValue();

Under older versions it's a bit different but not fundamentally so.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 24
Reputation: ray_broome is an unknown quantity at this point 
Solved Threads: 0
ray_broome ray_broome is offline Offline
Newbie Poster

Re: Using operator[] with an ArrayList

 
0
  #4
Mar 8th, 2005
Originally Posted by jwenting
An ArrayList isn't an array. It (nor Vector which you shouldn't use, it's there for legacy use only) has different mechanisms.

To retrieve an item from your List use the get(index) method.

Under Java 1.5 (5.0, Tiger) you should thus do the following:

  1. ArrayList<Item> itemsList = new ArrayList<Item>();
  2. ...
  3. Item anItem = new Item();
  4. ...
  5. itemsList.add(anItem);
  6. double amount = itemsList.get(0).getStartValue();

Under older versions it's a bit different but not fundamentally so.
Thanks, i realized i would have to do it this way after a little tinkering. I am coming from C++ so i have to get used to certain things in Java ( I miss my operator overloading :cry: )
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 4
Reputation: aviasoorya is an unknown quantity at this point 
Solved Threads: 0
aviasoorya aviasoorya is offline Offline
Newbie Poster

Re: Using operator[] with an ArrayList

 
0
  #5
Dec 3rd, 2006
hi
i guess its not a problem with the code, it must function i see ur code is this
ArrayList<Item> itemsList = new ArrayList<Item>();
...
Item anItem = new Item();
...
itemsList.add(anItem);
double amount = itemsList.get(0).getStartValue();
and i understand from this is
u r trying to add item object in the array list and
then u r trying to access them with them
and
double amount = itemsList.get(0).getStartValue();
will fetch you the value of item object stored at 0th index of array and gives u the Value of getstartvalue();

i would make in little bit different way

for(int i = 0; i <itemsList.size(); i++)
{
Item item = (Item)itemsList[0];
double amount = item .getStartValue();
}


reply me if i have not answered to your point
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Using operator[] with an ArrayList

 
0
  #6
Dec 4th, 2006
I'm sorry, but why are you posting an answer to a question that is 18 months old, and, according to the OP's last response, is solved.

And your answer is wrong, I might add. You are attemtping to use array indexing into an ArrayList, which won't work, and is exactly the problem that was already solved in this thread.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Using operator[] with an ArrayList

 
0
  #7
Dec 4th, 2006
and just as important as trying to give good advise is to try and word it in proper English.

1337 sp33ch and SMS shorthand are NOT proper English.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC