Dan
Dan

Reputation: 89

LinkedLists trying to call a class

I'm very new to linked lists but currently I have a linkedlist of objects and was lost on how I would call's method another by using one of the objects in the linked list.

public Store() {
    products.add(new Product("Whiteboard Marker", 85, 1.50));
    products.add(new Product("Whiteboard Eraser", 45, 5.00));
    products.add(new Product("Black Pen", 100, 1.50));
    products.add(new Product("Red Pen", 100, 1.50));
    products.add(new Product("Blue Pen", 100, 1.50));

}

these are my current objects in the linkedlist.

I have a class called product with a function getName.

public String getName() {
    return this.name;
}

So I was wondering how when calling the function getName, it would return "Black Pen"

thank you.

Upvotes: 2

Views: 86

Answers (3)

Amit
Amit

Reputation: 32386

If i understand it correctly, you have a list of product objects, which has a getter for name and you want to get the name of product, while its there in the Arraylist. According to this assumption i created a dummy person and ArrayList and calling the getter of product and printing it.

If you know the location of the object in the ArrayList then its easy to print it, just by giving the index of object in the ArrayList. otherwise if you know the some unique property of the person, then you can use if condition to filter on that property.

I've added both the case and included that in comment section.

   class Person {
    private String name;
    private String location;

    public Person(String name,String location) {
        this.name = name;
        this.location = location;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
}

public class Test {
    public static void main(String[] args) {

        List<Person> productList = new ArrayList<>();
        productList.add(new Person("Amit","india"));
        productList.add(new Person("A", "bangalore"));

        // case 1 :- when you know the location of person in LL.
        System.out.println(productList.get(0).getName());

        // case 2:- when you know some unique peroperty of person and filtering on base of this.
        for(Person product : productList){
            if(product.getLocation().equalsIgnoreCase("india")){
                System.out.println("name of person " + product.getName());
            }
        }
    }
}

Output :-
Amit
name of person Amit

Upvotes: 1

Jay Smith
Jay Smith

Reputation: 2490

To call a method of object stored in LinkedList you have to get this object from list. To take first element of linked list

products.getFirst().getName();

To take first element of linked list

products.getLast().getName();

Note that you can get elements from linked list only sequentially starting from first or from last.

Upvotes: 0

RaceYouAnytime
RaceYouAnytime

Reputation: 734

In your case, to get "Black Pen," you would write:

products.get(2).getName();

Upvotes: 1

Related Questions