gpt4 book ai didi

java - print() 方法未打印正确的 ArrayList 内容

转载 作者:行者123 更新时间:2023-11-30 07:47:40 25 4
gpt4 key购买 nike

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

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中的内容不正确,我不知道为什么。有人可以指出我的愚蠢错误吗?谢谢。

编辑:当我打印时,它仅打印最后一个数组列表的整数数组(当我在 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()
{

}

}

最佳答案

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

binary = values.get(i-1);

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

values.add(binary);

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

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

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

关于java - print() 方法未打印正确的 ArrayList 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33700408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com