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

  • SEARCH
  • Home
  • 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 9221169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:31:08+00:00 2026-06-18T03:31:08+00:00

I want to do something simple which is create my own list class that

  • 0

I want to do something simple which is create my own list class that is based on java.util.linkedlist but i want to limit what can be done. Lets just say that for now, the only method I want available is the addFirst method. I’m creating somewhat of a singly linked list. The issue i’m having is that addFirst doesnt seem to be recognized and i’m not sure if i’m creating the list properly inside my own constructor. Here is the code I have so far.

import java.util.LinkedList;


public class singleList {

singleList() {
     new LinkedList();
 }

//  void add(name addName) {
//      addFirst(addName);
//  }


public static void main(String[] args) {

singleList nameList = new singleList();

 name name1 = new name("John", "Smith");
 nameList.add(name1);


System.out.println("LinkedList contains : " + nameList);

 }
}

So as you can see what I want to do is create a list called nameList and at this point, the ONLY thing it can do is use the add method, which should call the LinkedList’s addFirst method which adds the element to the first position in the list. Does this make sense?

The first error I get is that it cannot find the symbol “addFirst” from my add method and i’m also not sure if my constructor for singleList is correct.

In case anyone is wondering, this is for a school assignment.

Here is the revised code, which I took some advise from the first answer here which looks similiar to another.

public class SingleList {

private LinkedList internalList;

SingleList() {
     internalList = new LinkedList();
 }

void add(name addName) {
    internalList.addFirst(addName);
}

String get(int index) {

    return internalList.get(index).toString();

}


public static void main(String[] args) {

    SingleList nameList = new SingleList();

    name name1 = new name("John", "Smith");
    nameList.add(name1);

    String getter = nameList.get(0);  
    System.out.println("LinkedList contains : " + nameList.get(0));

}
}
  • 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-06-18T03:31:09+00:00Added an answer on June 18, 2026 at 3:31 am

    The problem with the code you posted is that you aren’t assigning the LinkedList to anything, then the addFirst() method is written as if it’s a static method of the singleList class, which doesn’t exist. What I believe you were trying to do is:

    import java.util.LinkedList;
    
    public class SingleList<T>
    {
        private LinkedList<T> internal;
    
        SingleList()
        {
            this.internal = new LinkedList<>();
        }
    
        public void add(T addName)
        {
            this.internal.addFirst(addName);
        }
    
        @Override
        public String toString()
        {
            if(!this.internal.isEmpty())
            {
                return this.internal.get(0).toString();
            }
            else
            {
                return this.internal.toString();
            }
        }
    
        public static void main(String[] args)
        {
            SingleList<name> nameList = new SingleList<>();
            name name1 = new name("John", "Smith");
            nameList.add(name1);
            System.out.println("LinkedList contains : " + nameList);
        }
    }
    

    This also has the benefit of generics, which means it can use any type which LinkedList can use. Additionally, I took the liberty of changing the case of the class name, as the Java standard is to use proper cases on Class names, and sulking case on variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement something with jquery, simple javascript and java but I don't
I want to load this simple something into my Editor: Write:-repeat,write(hi),nl,fail. So that it
I've built a simple cms with an admin section. I want to create something
I need to do something which i expected to be was simple - create
Hello I want to create a simple blog where users can post comments, but
I want to create something that resembles an extendable Enum (understanding extending Enums isn't
I have a simple class which does some calculations in its own thread and
I want to something as simple as turning this is a test into new
I want to do something simple in assembly language. addition two numbers, and print
I want to implement something simple like /System/Workflows/Sample Workflow with the small addition of

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.