print()方法无法打印正确的ArrayList内容

时间:2015-11-13 19:29:26

标签: java arraylist

我的代码将十进制转换为二进制,如下所示:

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);
    }

正如您所看到的,values类型的ArrayList<int[]>对象存储了i的二进制值(来自实例化的整数数组binary,它是通过每个循环创建的 I 的)。但是,在打印每个i的二进制数的所有整数数组表示后,立即从列表中提供以下输出:

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

我的print()部分代码运行正常。 ArrayList中的内容不正确,我不知道为什么。有人可以指出我的愚蠢错误吗?感谢。

编辑:当我打印时,它仅打印LAST arraylist的整数数组(当我在for循环后打印时)。但是,当我打印刚刚添加到for循环中的数组列表中的内容时,其内容是正确的。

完整代码:

    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()
{

}

}

1 个答案:

答案 0 :(得分:1)

损害是由无害的陈述完成的

binary = values.get(i-1);

访问上一次迭代中添加的int[]末尾的List<int[]>。现在binary 引用到该整数数组,并且以下操作修改存储在上一次迭代中的数组。最后,

values.add(binary);

将对该数组的引用添加为列表的另一个元素,无论它有多少元素,它都引用与所有存储引用相同的int []。

修复很简单:不是将引用复制到数组对象,而是复制数组本身

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