【问题标题】:print() method not printing correct ArrayList contentsprint() 方法未打印正确的 ArrayList 内容
【发布时间】:2015-11-13 19:29:26
【问题描述】:

我的代码像这样将十进制转换为二进制:

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

如您所见,ArrayList&lt;int[]&gt; 类型的 values 对象存储 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()
{

}

}

【问题讨论】:

  • 你想做什么?或者什么不工作?我不确定你的代码是否也能编译...
  • 你想完成什么?你可以改用Integer.toBinaryString(int i) 吗?
  • @aguibert 我只是在练习
  • 什么是 numV?它在哪里定义?这似乎不是所有的代码。
  • @AjeetKljh 问题是您执行binary = values.get(i-1); 并获得对您创建的第一个 int 数组的 reference 并将其存储到所有 List 元素中。所以,最后,所有 16 个 List 元素都指向同一个 int 数组,当然最终所有元素都设置为 1。 - 你需要 copy array,而不是数组对象引用。

标签: java arraylist


【解决方案1】:

损害是由看似无害的声明造成的

binary = values.get(i-1);

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

values.add(binary);

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

解决方法很简单:不要将引用复制到数组对象,而是复制数组本身

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2012-06-29
    • 2013-07-19
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    相关资源
    最近更新 更多