Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6567605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:21:28+00:00 2026-05-25T14:21:28+00:00

I have created a linkedlist which stores two elements. Can someone tell me how

  • 0

I have created a linkedlist which stores two elements. Can someone tell me how to access the second element. In this case, the lastName

This is what I came up with so far.

public class Bank
{
  private LinkedList<List> words = new LinkedList<List>();
  public void startup()
   {
     words.add(new List("Fred","Cool"));
   }

This is my list class

public class List
{
   public List(String name, String lastName)
   {
     this.name = name;
     this.lastName = lastName;
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:21:28+00:00Added an answer on May 25, 2026 at 2:21 pm

    You have a number of problems in your code:

    1. I suggest you rename your List class to Person, as object of that type clearly does not represent lists.

    2. name and lastName will not be elements of the list. They are arguments to the constructor of the List (read Person) class.

    3. You have no fields in your List, so you can’t do

      this.name = name;
      this.lastName = lastName;
      

    To access the second element in the words list, you do

    words.get(1);   // access second element (indecies are 0-based)
    

    However, since you’ve only added one element you won’t be able to use the above expression.


    Here’s a complete example to get you on the right track:

    import java.util.LinkedList;
    
    class Bank {
        private LinkedList<Person> persons = new LinkedList<Person>();
    
        public void startup() {
            persons.add(new Person("Fred", "Cool"));
        }
    
        public LinkedList<Person> getPersonList() {
            return persons;
        }
    }
    
    class Person {
        String name, lastName;
    
        public Person(String name, String lastName) {
            this.name = name;
            this.lastName = lastName;
        }
    }
    
    class Test {
        public static void main(String[] args) {
            Bank bank = new Bank();
            bank.startup();
    
            String lastNameOfFirstPerson = bank.getPersonList().get(0).lastName;
            System.out.println(lastNameOfFirstPerson); // prints "Cool"
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this LINQ-query: // types... LinkedList<WeightedItem> itemScores = new LinkedList<WeightedItem>(); var result =
Let's assume we have a java.util.LinkedList containing a few hundreds of elements. One possibility
I have made a LinkedList to store State objects which is a class I
I have created two classes, class A that uses a Linked List and class
Suppose I have the following method, which can be used to create a collection
I have implemented a linked list as a self-referencing database table: CREATE TABLE LinkedList(
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a template for Visual Studio 2008 and it currently shows up
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a PHP-script to update a web server that is live inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.