I need some help in trying to find out how to cast a method that is within an object which is inside a LinkedList.

Let me try to explain:

I already know that I have to use a wrapper to convert an object back into another state. The think I am confused about is how to use a method from the object. What I am doing is storing Objects called Item into a LinkedList. I have a method from Item that gets a stored variable. Heres the code for my Item class.

public class Item implements Comparable
{
	private int myId;
	private int myInv;
	
	public Item(int id, int inv)
	{
		myId = id;
		myInv = inv;
	}
	
	public int getId() 
	{
		return myId;
	}
	
	public int getInv() 
	{
		return myInv;
	}
	
	public int compareTo(Object otherObject) 
	{
		Item right = (Item)otherObject;
		return myId - right.myId;
	}
	public boolean equals(Object otherObject) 
	{
		return compareTo(otherObject) == 0;
	}
	
	public String toString() 
	{
		String string = (getId() + "  " + getInv());
		
		return string;
	}
}

From my experience so far, Arrays with stored objects only need to method call after referencing it.

int id = myArray[index].getId()

What I tried was:

int id = myList.get(index).getId();

I knew that this aproach probably wouldn't work anyway. So right now I need someone to help me to understand how to call a method from an object within a LinkedList.


Thanks in advanced,

Lord Felix

Dani AI

Generated

Short answer: both approaches in the replies are correct. With a raw (pre‑generics) list the element type is Object at compile time so an explicit cast is required; with Java 5+ it’s better to declare the list as a typed collection so the compiler gives Item objects back and no cast is needed. and are on the right track.

When a cast is used, guard it to avoid a ClassCastException if the list can hold mixed types. For example: Object o = myList.get(i); if (o instanceof Item) { Item it = (Item)o; /* use it */ }. If the codebase is compiled for Java 5 or later, prefer generics and the enhanced for loop for clarity and safety: for (Item it : myList) { /* use it */ }.

One important practical point missing from the thread: LinkedList.get(index) runs in linear time because it must walk the list to the index. That makes frequent random access expensive. For sequential processing use an iterator/for‑each; for lots of indexed access prefer ArrayList instead.

A couple of small improvements to the Item design that will prevent bugs later: implement the generic form Comparable<Item> and change compareTo to take an Item; when equals is overridden also override hashCode; and consider making identity fields immutable (final) if appropriate. Also note LinkedList is not synchronized—wrap it or use a concurrent collection if multiple threads access it.

These points explain when casting is necessary, how to do it safely, and why switching to generics and the right list implementation usually leads to clearer, faster code.

Recommended Answers

All 2 Replies

You need to explicitly cast the object you retrieve from the List to the correct type unless you use a 1.5 level compiler in which case you can use generics to declare the List to only accept a specific type (and thus do the cast for you on retrieval).

You'd then declare the List as:

List<Item> myList = new LinkedList<Item>();

Hey there,

int id = myList.get(index).getId(); // yeah this line could work

The only problem is your storing objects, the list doesn't know at run time what kind of object is stored there i think. You would have cast it like jwenting said.

Item t = (Item)myList.get(index);
int id = t.getId();

This would work i think.

good luck,
Mel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.