Java – why does changing an object in an ArrayList change it in all other ArrayLists?

I'm making a CPU scheduling simulator (for school projects) There is a problem with my roundrobin function When I do c.get (I) jobTime – = 2; And C. get (I) jobTime – = 1; It affects my other ArrayLists, so I can't do my other functions My list was perfectly normal before I called roundrobin 2 Why is that?

For example, this is what my list4 looks like after roundrobin 2

Listing 4: [job101 0, job102 0, job103 0, job104 0, job105 0, job106 0]

This is how I read in the file and put the jobs object into my ArrayLists

Scanner input = new Scanner(new File("testdata1.txt"));
    ArrayList<Jobs> list = new ArrayList<Jobs>();
    ArrayList<Jobs> list2 = new ArrayList<Jobs>();
    ArrayList<Jobs> list3 = new ArrayList<Jobs>();
    ArrayList<Jobs> list4 = new ArrayList<Jobs>();


    Jobs first;

    while(input.hasNext())
    {
        first = new Jobs(input.next(),input.nextInt());
        list.add(first);
        list2.add(first);
        list3.add(first);
        list4.add(first);
    }

    input.close();

This is my roundrobin 2

public void roundRobin2(ArrayList<Jobs> c,int sT)
{   
    int size = c.size();
    int cT = 0;
    int ccT = 0;
    while(!c.isEmpty())
    {   
        int i = 0;
        System.out.println(c);
        for(i = 0; i < size; i++)
        {
            if((c.get(i).jobTime) >= 2)
            {
                c.get(i).jobTime -= 2;
                cT += 2;

                if((c.get(i).jobTime) == 0)
                {
                    ccT += cT;
                }
            }
            else
            {
                (c.get(i).jobTime) -= 1;        
                cT += 1;

                if((c.get(i).jobTime) == 0)
                {
                    ccT += cT;
                }
            }
        }
        for(i = 0; i < size; i++)
        {
            if((c.get(i).jobTime) == 0)
            {
                c.remove(i);
                size = c.size();
            }
        }
    }
    System.out.println("\nAverage completion times: " + ccT + "/" + sT +  " = " + ((ccT)/sT));
}

Solution

In each iteration, you create only one object and add it to all four lists When you change that object, you can change it This mutation will be reflected in all lists because they store the same object reference

while(input.hasNext())
{
    first = new Jobs(input.next(),input.nextInt());
    list.add(first);
    list2.add(first);
    list3.add(first);
    list4.add(first);
}

Instead, you need to add a new object reference to each list (if you want to store a clone of the object in each list)

while(input.hasNext())
{
    String s = input.next();
    int i = input.nextInt();
    list.add(new Jobs(s,i));
    list2.add(new Jobs(s,i));
    list3.add(new Jobs(s,i));
    list4.add(new Jobs(s,i));
}

In the first code example, list get(n)== list2. Get (n) will be true (true for any valid N and any two lists from 4) In the second example, it would be wrong because you now have completely unrelated objects that store the same value exactly where they were added to the list

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>