LinkedLists trying to call the class

I am very new to linked lists, but I currently have a linked list of objects and I am lost on how I would call a method on others 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 linked list.

I have a class called product with getName function.

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

      

So I was wondering how when calling getName function it will return "Black Pen"

thank.

+3


source to share


3 answers


If I understand correctly, you have a list of product objects that has a recipient for the name and you want to get the name of the product and its there in the Arraylist. According to this assumption, I created a dummy person

and ArrayList

and named the product getter and printed it out.

If you know the location of the object in ArrayList

, then it's easy to print it just by specifying the object's index in ArrayList

. otherwise, if you know some unique property of a person, you can use a condition if

to filter that property.



I added both cases and included that in the comments 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

      

+3


source


In your case, to get the Black Pen, you have to write:



products.get(2).getName();

      

+1


source


To call a method of an object stored in LinkedList

, you must get that object from the list. To take the first element of a linked list

products.getFirst().getName();

      

Get the first element of a linked list

products.getLast().getName();

      

Note that you can only get items from a linked list sequentially, starting with the first or the last.

0


source







All Articles