2

Ok so this is probably a very beginner question but...

Right now I have an object called Person with a method on it called setName

I am in my main class and I have declared

List People = new LinkedList();

What I want to do is simply call the setName function of the object in the first position. I have found that this is very easy to do when working with an array of People. All I would do would be to say

People[0].setName("steve");

But since it is in a linkedlist I am having troubles.

I tried using the "get" method like so...

People.get(0)

but then it doesn't allow me to use my user defined methods so I can't set the name.

What is the best way to get an object out of a linked list and access its methods and instance variables?

thanks in advance

7 Answers 7

7

You want to use generics to make it a list of people:

List<Person> People = new LinkedList<Person>();

People.get(0).setName("steve");
Sign up to request clarification or add additional context in comments.

1 Comment

Confusing names but ... yes, generics are the ticket.
4

Use generics:

List<Person> people = new LinkedList<Person>();
// …
people.get(0).setName("steve");

Comments

3

You should use generics: List<Person>. Then you would be able to use people.get(0).setName(..)

Note that Java naming convention requires your variable to be lowercase.

Comments

2

You dont set which type of objects you store. So you need upcast:

Person person = (Person)People.get(0);
person.setName("steve");

or set type of stored objects:

List<Person> people = new LinkedList<Person>();

Comments

2

LinkedList is only good when you want to iterate quickly over the list, and when you want to quickly get to the first or last element. Also, you need to properly define people as a LinkedList to have access to the linked list methods, and you should type your list, so:

LinkedList<Person> people = new LinkedList<Person>();
people.getFirst().setName();

However, ArrayList is a better general choice, which offers good performance when accessing elements by their index, so:

List<Person> people = new ArrayList<Person>();

then

people.get(0).setName("fred");

1 Comment

LinkedList is also faster at inserting and deleting elements in the middle of the list.
0

If using Java 1.4 or earlier you need to cast it to People class:

((Person)People.get(0)).setName("SomeName");

If using Java 1.5 or later use generics:

List<Person> people = new LinkedList();

1 Comment

Um, don't you mean List<Person> people = new LinkedList<Person>(); for the generics version?
0

You need to define the Linked list as a Person LinkedList:

LinkedList<Person> people = new LinkedList<Person>();
people.get(0).setName("Steve");

Another note(not about the question, just to improve your Java): You use lowercase on the first letter of variable names, because of Java conventions and any subsequent word's first letter are capitalized;

float personsAge = 57.47;

EXPLANATION(Ignore if easily bored):

The LinkedList was not set to store a specific variable type. Another way to have solved this problem would have been:

LinkedList people = new LinkedList();
Person person = (Person) people.get(0);
person.setName("steve2")

Although you would have to do this to each 'Person' in the people list, defeating the point of the LinkedList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.