•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 429,786 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,931 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 4901 | Replies: 6
![]() |
•
•
Join Date: Aug 2004
Posts: 19
Reputation:
Rep Power: 5
Solved Threads: 0
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:
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?
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:
ArrayList itemsList = new ArrayList(); ... itemsList.add(anItem); 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?
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
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();
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();
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 199
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:
Under older versions it's a bit different but not fundamentally so.
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:
ArrayList<Item> itemsList = new ArrayList<Item>(); ... Item anItem = new Item(); ... itemsList.add(anItem); double amount = itemsList.get(0).getStartValue();
Under older versions it's a bit different but not fundamentally so.
•
•
Join Date: Aug 2004
Posts: 19
Reputation:
Rep Power: 5
Solved Threads: 0
•
•
•
•
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:
ArrayList<Item> itemsList = new ArrayList<Item>(); ... Item anItem = new Item(); ... itemsList.add(anItem); 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: ) •
•
Join Date: Nov 2006
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
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.
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
----------------------------------------------
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Need Help with ArrayList sorting (Java)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the Java Forum
- Previous Thread: ActionListener
- Next Thread: Isn't java pass by reference?



Linear Mode