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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:27:33+00:00 2026-06-13T00:27:33+00:00

I have an home work,I used an array and element of array is linked

  • 0

I have an home work,I used an array and element of array is linked list because the element in a row is not fixed need delete or add some time depend on problem condition , I was tried these codes below, but I have a problem when adding new element to a fixed row for example p[0] the value will be added for all, how I can solve this problem please help.

public class schedule
{
   public class link
   {
     public LinkedList <Integer>list = new LinkedList<Integer>() ;
     public link(LinkedList<Integer> value)
    {
        list = value;
    }
    public link(int value)
    {
        list.add(Integer.valueOf(value)) ;
    }

}


private link p[] = new link[10];
public schedule()
{
    LinkedList<Integer> l = new LinkedList<Integer>();
    l.add(Integer.valueOf(2));
    l.add(Integer.valueOf(0));
    l.add(Integer.valueOf(3));
    for(int j=0;j<p.length;j++)
        p[j] = new link(l);



    p[0].list.add(9); // here I  have problem

    for(int j=0;j<p.length;j++)
    {
        System.out.print("p["+j+"]:");
        for(int i=0;i<p[j].list.size();i++)
            System.out.print(p[j].list.get(i).intValue());
        System.out.println();
    }

}
public static void main(String []arg)
{
    new schedule();
}


the output is like this : the value 9 added to all but I want to be added just for first element
p[0]:2039
p[1]:2039
p[2]:2039
p[3]:2039
p[4]:2039
p[5]:2039
p[6]:2039
p[7]:2039
p[8]:2039
p[9]:2039
  • 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-13T00:27:34+00:00Added an answer on June 13, 2026 at 12:27 am

    The problem is that you’re initializing every link instance with the same LinkedList<Integer>:

    LinkedList<Integer> l = new LinkedList<Integer>();
    l.add(Integer.valueOf(2));
    l.add(Integer.valueOf(0));
    l.add(Integer.valueOf(3));
    for(int j=0;j<p.length;j++)
    //it will be the same LinkedList<Integer> l per all the instances in the array
        p[j] = new link(l);
    

    Take note that when you do this, every link instance will have the list attribute referencing the same LinkedList<Integer> l variable. So, if you modify it in one place, everyone will be updated (because it’s the same reference).

    There are many ways to solve this:

    • Create a new LinkedList on every iteration of your for loop.
    • Create the new link instance using another constructor.
    • Fill your link[] p array one by one…
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a desktop at work, and a laptop at home. I do not
In my home work of Ackermann function I have solved the problem as following
I'm doing some JAVA coding at home and at work. At home i have
I was doing some work at home over the weekend and used git to
They seem complex and unnecessary. The applications I've built at work or home have
We have a class home work for design pattern class. In that we have
I have a homework task to prove whether or not a particular variation on
I have a homework assignment where I need to take input from a file
I have a homework question that says: Problem 1: Given the array [ 22
I have a homework assignment to sort an array in ascending order. Obviously, this

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.