我有一个arrayList<Integer>
和arrayList<JLabel>
。 Integer 以整数形式保存货币,而 JLabel 以字符串形式保存相同的值。我想从两个标签中随机删除元素。例如,如果在 JLabel 中删除 20TL(tl 是货币),我也想在整数 arrayList 中删除它。这很简单。但后来我想计算 ArrayList 中剩余资金的平均值。这是我的另一个 arrayList,用于随机排列 0 到 23 个数字。因此,我删除了 IntList 和 JLabel 列表中的相同元素。
ArrayList<Integer> Numbers = new ArrayList<Integer>();
for(int n = 0; n<24; n++){
Numbers.add(n);
}
Collections.shuffle(Numbers);
然后这是我的两个列表。
ArrayList<Integer> Money = new ArrayList<Integer>();
Money.add(1); Money.add(5); Money.add(10); Money.add(25); Money.add(50); Money.add(100); Money.add(500); Money.add(1000); Money.add(2000);
Money.add(5000); Money.add(10000); Money.add(20000); Money.add(25000); Money.add(30000); Money.add(40000); Money.add(50000); Money.add(100000); Money.add(200000);
Money.add(300000); Money.add(400000); Money.add(500000); Money.add(750000); Money.add(1000000); Money.add(2000000);
String[] para =new String[] {"1 TL","5 TL","10 TL","25 TL", "50 TL","100 TL","500 TL",//create an array for moneys
"1.000 TL","2.000 TL","5.000 TL","10.000 TL","20.000 TL","25.000 TL",
"30.000 TL","40.000 TL","50.000 TL","100.000 TL",
"200.000 TL","300.000 TL","400.000 TL","500.000 TL","750.000 TL"
,"1.000.000 TL","2.000.000 TL"};
ArrayList <JLabel> myLabel = new ArrayList<JLabel>();
for(int i=0; i < 12 ; i++){
JLabel holder = new JLabel();
holder.setText(para[i]);
myLabel.add(holder);
p2.add(holder);//add the label to the panel
}
for(int j=12; j<para.length; j++){
JLabel holder2 = new JLabel();
holder2.setText(para[j]);
myLabel.add(holder2);
p3.add(holder2);
}
这是我在actionListener方法中删除style.this
private int asd = 0;
///// some code
myLabel.get(Numbers.get(asd)).setVisible(false);
Money.remove(Numbers.get(asd));
当我尝试删除 intarraylist 中的钱时,计算方法无法正常工作。因为例如如果 Numbers 数组的第一个元素是 5,那么 50 将被删除。并且 arrayList 将会缩小。之后,当 Numbers.get(asd) 等于 23 时,int arraylist 中将不再有第 23 个元素。因为它缩小了并且没有这样的第23个元素。我希望我能很好地讲述我的问题。
Ps:我尝试使用数组而不是数组列表。但我无法计算左边的平均值。因为当删除某些元素时数组不会收缩。
最佳答案
我会对该代码进行很多更改。其一,我会尝试创建一个值集合,这样我就不必费力地更改并行集合,因为我知道这会减少出错的机会。对于这样的事情,我会使用 JList<Integer>
并填充其模型,a DefaultListModel<Integer>
与整数。然后,我可以使用设置为土耳其语言环境的 NumberFormat 货币实例轻松地将其显示为土耳其里拉。例如,如果我像这样创建模型:
// constants used to populate my model
private static final Integer[] VALUES = { 1, 5, 10, 25, 50, 100, 500, 1000, 2000, 5000, 10000,
20000, 25000, 30000, 40000, 50000, 100000, 200000, 300000, 400000, 500000, 750000,
1000000, 2000000 };
// locale for Turkey used to get its currency
private Locale trLocale = new Locale("tr", "TR");
// currency number formatter for Turkish Lira
private NumberFormat tlFormat = NumberFormat.getCurrencyInstance(trLocale);
// my JList's model
private DefaultListModel<Integer> listModel = new DefaultListModel<>();
// create the JList with its model
private JList<Integer> jList = new JList<>(listModel);
// elsewhere in my constructor
// populate my list model with the array values
for (Integer value : VALUES) {
listModel.addElement(value);
}
// set my JList's renderer to render the numbers as Turkish Lira
jList.setCellRenderer(new MyCellRenderer());
// add my list to a JScrollPane and set how many rows are visible
jList.setVisibleRowCount(10);
JScrollPane scrollPane = new JScrollPane(jList);
JList 的单元格渲染器会将其保存的值(此处为整数)更改为土耳其里拉的字符串表示形式:
private class MyCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
String textValue = tlFormat.format(value); // format the Integer to its currency String
// pass it into the super's renderer
return super.getListCellRendererComponent(list, textValue, index, isSelected, cellHasFocus);
}
}
然后,如果稍后我可以从按钮的 ActionListener 中的列表中删除选定的值,并让它调用一个计算列表中所有项目的平均值的方法:
@Override
public void actionPerformed(ActionEvent e) {
List<Integer> selectedValues = jList.getSelectedValuesList();
for (Integer selectedValue : selectedValues) {
listModel.removeElement(selectedValue);
}
// method elsewhere that iterates through the listModel, calculates an average
// and displays it
calculateAndDisplayAverage();
}
它可能看起来像这样:
关于Java如何计算随机删除一些元素的arrayList的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43283279/