Reputation: 119
I want to load a series of objects to an ArrayList, and return the index of where it was added so it can be used later directly from the ArrayList. It'd be akin to me loading a set of 5 frames for a graphic and calling the data for frame 3 directly from the list.
Upon looking around at a couple potential posts that may be related, I found something that was related, and made me wonder. Is there even a built in function to GET the index of a recently added object?
The link I am looking at that made me think of this was: ArrayList indexOf() returns wrong index?
Basically, the way I was looking at it was that I would do something along the lines of the following psuedocode:
private ArrayList<FrameData> mylistofframeshere = new ArrayList();
FrameData Framenumberone = new FrameData(constructorblah goes in here);
int wherediditgo = mylistofframeshere.add(Framenumberone);
Or I thought I could do something along the lines of
mylistofframeshere.getindex(Framenumberone);
My backgrounds in coding are more procedural based at this point, so I am more used to knowing what the index is, just in order to place the data to begin with. I looked around at the oracle documentation as well, with findings something similar to the above link. Any suggestions??
EDIT : I'm going to add some extra clarification in here, because I really didn't put enough effort into the example and explanation.
Another example of trying to use something like a direct index of a list would be if I had a set of tiles I wanted to use as terrain for a game or something. I would load a set of possible tiles for the area into a central ArrayList. Upon trying to place these tiles, I would just reference the pre-loaded object I have in my ArrayList in order to draw the appropriate bitmap/whatever.
I'm still a bit new to Java, so I'm willing to bet it's just something simple I'm overlooking in the mechanics of using these datatypes. I'll keep editing this until I get the explanation right.
Upvotes: 2
Views: 10332
Reputation: 2854
Instead of using ArrayList
in this way, you can use a Map<Integer,FrameData>
. you can replace Integer
with anything which might fit better in your project.
Upvotes: 0
Reputation: 23186
It all depends on what you want to use the index for. If you simply need to map each element in your list to a key so you can later retrieve the element with the key, you should perhaps consider using a HashMap. If you are more concerned with the ordering, you can use a List. As someone already answered, the index with be incremented as you add elements into the list. If you know the total number of frames you will have before hand, you can initialize an ArrayList by passing in the size as an argument to its constructor, then add each frame by manually specifying the index with list.add(0...size-1, element).
In short,
Upvotes: 0
Reputation: 34301
There is a method like ArrayList.indexOf(object);
, Try using that method to get index of the object
Upvotes: 2
Reputation: 328795
When you add something to an ArrayList
, it goes to the last available space. In other words:
List<FrameData> list = new ArrayList<FrameData>();
list.add(frame1);
FrameData frame = list.get(list.size() - 1); //frame == frame1
But I wonder why you would need to do that. If you explain more about what you are trying to achieve, there might be a different / better way to get to the same result.
Upvotes: 6