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 6071733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:04:52+00:00 2026-05-23T10:04:52+00:00

I have been trying to write a Java function IntList get(int i) that is

  • 0

I have been trying to write a Java function IntList get(int i) that is supposed to return a reference to the i-th element in a singly linked integer list.
My problem is that the function returns null, even if I try to reference an existing element!

public class IntList {

    private int info;                   //the int data of this list element  
    private IntList next;                   //the rest of the list

    /**
    * Sets up a new instance of IntList corresponding to the given info and next.
    * @param info the int data of this list element
    * @param next the rest of the list
    */
    public IntList(int info, IntList next) {
        this.info = info;
        this.next = next;

    }

    /**
    * A new list where the given info has been prepended.
    * @param info the int data of the new list element
    * @return a new instance of IntList
    */
    /*
    public IntList prepend(int info) {
        return new IntList(info, this);

    }
    */

    /** 
    * A new list where the given info has been appended.
    * @param info the int data of the new list element
    * @return a new instance of IntList
    */
    public IntList append(int info) {
        if(next == null) {
            return new IntList(this.info, new IntList(info, null));

        } else {
            return new IntList(this.info, next.append(info));

        }
    }

    /**
    * Commputes the sum of all elements of this list.
    * @return the sum of all elements
    */
    public int sum() {
        if(next == null) {
            return info;

        } else {
            return info + next.sum();

        }
    }

    /**
    * Auxiliary function for the reversal of this list.
    * @param acc the list elements accumulated so far
    * @return a new instance of IntList
    */
    private IntList reverseAux(IntList acc) {
        if(next == null) {
            return new IntList(info, acc);

        } else {
            return next.reverseAux(new IntList(info, acc));

        }
    }

    /**
    * A new list with the elements of this list in reverse order. 
    * @return   a new instance of the IntList
    */
    public IntList reverse() {
        return reverseAux(null);

    }

    /**
    * String representation of this list.
    */
    @Override
    public String toString() {
        if(next == null) {
            return "" + info;

        } else {
            return info + " , " + next;

        }
    }

    /**
    * An integer array is converted to a list
    * @param values is an array containing integer elements
    * @return a new instance of IntList
    */
    public static IntList fromArray(int[] values) {
        int n = values.length;
        IntList res = new IntList(values[0] , null);

        for(int i = 1; i < n ; i ++) {
            res = res.append(values[i]);

        }
        return res;

    }

    /**
    * The length of a given IntList object is determined
    * @return the length of the list
    */
    public  int length() {
        int counter = 1;

        while(next != null) {
            counter = counter + 1;

            next = next.next;
        }
        return counter;
    }

    public IntList get(int i) {


        for(int k = 0 ; k < i - 1 ; k ++) {

            if(next != null) {
                next = next.next;
            }

        }
        return next;

    }




    public static void main(String[] args) {
        IntList lst = new IntList(1, null);

        for(int i = 2 ; i < 10 ; i ++) {
            lst = lst.append(i);    
        }

        System.out.println(lst);
        System.out.println(lst.reverse());
        System.out.println(lst.sum());

        int[] values = new int[4];
        values[0] = 3;
        values[1] = 4;
        values[2] = 5;
        values[3] = 8;

        System.out.println(fromArray(values));
        System.out.println(lst.length());
        System.out.println(fromArray(values).length());
        System.out.println(lst.get(2));

    }   




}

Since my implementation of the list does not require two separate classes for nodes and the list itself, I cannot find any valuable information on the web (most people use two classes).

  • 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-23T10:04:52+00:00Added an answer on May 23, 2026 at 10:04 am

    This one works without checking for IndexOutOfBoundsException:

    public IntList get(int i) {
        IntList current = this;
    
        for(int k = 0 ; k < i - 1 ; k ++) {
    
            if(current.next != null) {
                current = current.next;
            }
    
        }
        return current;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get around this error for a day now and
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I have been trying to determine a best case solution for registering a COM
I have been trying to work my way through Project Euler, and have noticed
I have been trying to explain the difference between switch statements and pattern matching(F#)
I have been trying to read a picture saved in Access DB as a
I have been trying to learn multi-threaded programming in C# and I am confused
I have been trying to make a case for using Python at my work.
I have been trying to learn how to add testing to existing code --

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.