Andrew Kor
Andrew Kor

Reputation: 603

print() method not printing correct ArrayList contents

My code converts decimal to binary like so:

for(int i = 0; i < combinations; i++)
    {
        int binary[] = new int[numV];
        if(i == 0)
        {
            for(int j = 0; j < numV; j++)
            {
                binary[j] = 0;
            }
        }
        else if(i > 0)
        {
            binary = values.get(i-1);
            for(int a = numV-1; a >= 0; a--)
            {
                System.out.println(a);
                if(binary[a]==0)
                {
                    binary[a]++;
                    break;
                }
                else
                    binary[a]=0;
            }
        }
        values.add(binary);
    }

As you can see, the values object of type ArrayList<int[]> stores the binary value of i (from the instanced integer array binary that gets created with each loop of i). However immediately after printing all the integer array representation of the binary number for each i, from the list, it gives the following output:

0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000
0000

My print() part of the code works fine. The contents in the ArrayList are incorrect and I don't know why. Can someone point out my silly mistake? Thanks.

EDIT : When I print, it prints the LAST arraylist's integer array only (when i print after the for loop). However when I print what has been just added to the array list within the for loop, its contents are correct.

COMPLETE CODE:

    import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class KMap extends JFrame
{
    private ArrayList<String> variableNameList = new ArrayList<String>();
    private String [] characters = {"A","B","C","D","E","F","G","H","I","J"};
private ArrayList<int[]> values = new ArrayList<int[]>();
public KMap() 
{
    setTitle("Karnaugh Map for COMP 228");
    setLayout(new BorderLayout());
    createTopPane();
    updateVarialbes(4);
    pack();
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    setLocationRelativeTo(null);
}
public void createTopPane()
{
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new GridLayout(1,10));
    JLabel numVariablesString = new JLabel();
    numVariablesString.setText("# Variables => ");
    topPanel.add(numVariablesString);
    JButton[] variableButton = new JButton[9];
    for(int i = 0; i < variableButton.length; i++)
    {
        int numV = i+2;
        variableButton[i] = new JButton();
        variableButton[i].setText(Integer.toString(numV));
        variableButton[i].addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) 
            {
                removeLeftPane();
                removeRightPane();
                updateVarialbes(numV);
            }
        });

        topPanel.add(variableButton[i]);
    }
    add(topPanel, BorderLayout.NORTH);
}
public void updateVarialbes(int numV)
{
    int combinations = (int) Math.pow(2,numV) + 1;
    System.out.println(combinations);
    System.out.println("New variables: " + numV);
    values.clear();
    variableNameList.clear();
    for(int i = 0; i < numV; i++)
        variableNameList.add(characters[i]);
    System.out.println();
    for(int i = 0; i < combinations; i++)
    {
        int binary[] = new int[numV];
        if(i == 0)
        {
            for(int j = 0; j < numV; j++)
            {
                binary[j] = 0;
            }
        }
        else if(i > 0)
        {
            binary = values.get(i-1);
            for(int a = numV-1; a >= 0; a--)
            {
                System.out.println(a);
                if(binary[a]==0)
                {
                    binary[a]++;
                    break;
                }
                else
                    binary[a]=0;
            }
        }
        values.add(binary);
    }

    for(int i = 0; i < values.size(); i++)
    {
        for(int j = 0; j < values.get(i).length; j++)
            System.out.print(values.get(i)[j]);
        System.out.println();
    }



    createLeftPane(numV);
    createRightPane(numV);
}
public void createLeftPane(int numV)//numV = number of variables to display
{
    JPanel leftPanel = new JPanel();
    leftPanel.setLayout(new GridLayout(2,2));



    JLabel variableLabel[] = new JLabel[numV];
    JPanel topLeftInnerPanel = new JPanel();
    topLeftInnerPanel.setLayout(new GridLayout(1,numV));
    for(int i = 0; i < numV; i++)
    {
        variableLabel[i] = new JLabel();

        variableLabel[i].setText(variableNameList.get(i));
        topLeftInnerPanel.add(variableLabel[i]);
    }
    leftPanel.add(topLeftInnerPanel);



    JPanel topRightInnerPanel = new JPanel();
    JLabel functionLabel = new JLabel();
    String s = "F(";
    for(int i = 0; i < numV; i++)
    {
        s+=characters[i];
        if(i!=numV-1)
            s+=",";
    }
    s+=(")");
    System.out.println(s);
    functionLabel.setText(s);
    topRightInnerPanel.add(functionLabel);
    leftPanel.add(topRightInnerPanel);




    JPanel innerLeftPanel = new JPanel();
    innerLeftPanel.setLayout(new GridLayout(values.size(), 1));
    JPanel[] innerLeftValuesPanel = new JPanel[values.size()];
    for(int i = 0; i < values.size(); i++)
    {
        innerLeftValuesPanel[i] = new JPanel();
        innerLeftValuesPanel[i].setLayout(new GridLayout(1, numV));
        for(int j = 0; j < values.get(i).length; j++)
        {
            JLabel l = new JLabel();
            l.setText(Integer.toString(values.get(i)[j]));
            innerLeftValuesPanel[i].add(l);
        }
    }

    for(int i = 0; i < (2^numV); i++)
        innerLeftPanel.add(innerLeftValuesPanel[i]);
    leftPanel.add(innerLeftPanel);


    JPanel innerRightPanel = new JPanel();
    innerRightPanel.setLayout(new GridLayout(2^numV, 1));

    add(leftPanel);

}
public void removeLeftPane()
{

}
public void createRightPane(int numV)
{

}
public void removeRightPane()
{

}

}

Upvotes: 1

Views: 195

Answers (1)

laune
laune

Reputation: 31290

The damage is done by the innocuous looking statement

binary = values.get(i-1);

accessing the int[] at the end of the List<int[]> that was added in the previous iteration. Now binary refers to that integer array, and the following operations modify the array stored in the previous iteration. Finally,

values.add(binary);

adds the reference to that array as another element to the list, which, no matter how many elements it has, refers to the same int[] with all stored references.

The fix is simple: rather than copying a reference to an array object, copy the array itself.

binary = Arrays.copyOf(values.get(i-1), numV );

Upvotes: 1

Related Questions