user1580096
user1580096

Reputation: 487

Same element gets inserted in the LinkedList in java

When I create a LinkedList and insert a new element the new element gets inserted each time but when I dequeue the elements I get only the last element inserted. My code is something like this in JAVA.

import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
    public static void main(String[] args) throws IOException {
        int i,j,k,l,m,n,t;
        LinkedList q = new LinkedList();
        arc c = new arc();
        c.start = 1;
        c.end  =1 ;
        q.add(c);

        arc d = new arc();
        d.start = 2;
        d.end = 2;
        q.add(d);

        arc a1 = (arc) q.get(0);
        arc a2 = (arc) q.get(1);
        System.out.println(a1.start);
        System.out.println(a2.start);
    }
    public static class arc
    {
        public static int start;
        public static int end;
    }
}

The desired output was 1 and 2 but I am getting 2 and 2. Can someone help me with this problem. Thank You.

Upvotes: 1

Views: 118

Answers (5)

lol
lol

Reputation: 3390

Because you are setting static variable in arc. static variables are class variables, not instance variables. So all instances of arc will give same value for class variable.

Try this:

public static class arc
    {
        public int start;
        public int end;
    }

Actually same element is not getting inserted in list, two different elements (instances) are being inserted in list, but you are printing class variable value from both instances.

Upvotes: 3

Sebastiaan van den Broek
Sebastiaan van den Broek

Reputation: 6331

The fields in your arc class are static. I would start using a good IDE such as Eclipse so you will be warned against errors like these. Additionally there is much to improve in your code. For example, why use a static arc class? It doesn't look like you intend to use this class statically from outside the outer class. You should also use generics and capitalize the name of the arc class.

Upvotes: 1

Daniel
Daniel

Reputation: 36710

You are using static variables for start and end. Separate both classes into two source files and remove the static:

public class Lala {
    public static void main(String[] args) throws IOException {
        int i, j, k, l, m, n, t;
        LinkedList q = new LinkedList();
        arc c = new arc();
        c.start = 1;
        c.end = 1;
        q.add(c);

        arc d = new arc();
        d.start = 2;
        d.end = 2;
        q.add(d);

        arc a1 = (arc) q.get(0);
        arc a2 = (arc) q.get(1);
        System.out.println(a1.start);
        System.out.println(a2.start);
    }

}

arc.java:

public class arc
    {
        public int start;
        public int end;
    }

Upvotes: 0

Ezzored
Ezzored

Reputation: 925

Look at your code:

public static class arc
{
    public static int start;
    public static int end;
}

Your start and end variables are static, so there is only one start and end variable for any arc object.

Simply remove the static modifier, and it should work.

Upvotes: 0

codingenious
codingenious

Reputation: 8653

remove static from definition of start and end in arc class, else start and end will be class level variables.

refer to difference between static and non static

Upvotes: 0

Related Questions