2017-04-07 87 views
0

我有一个arrayList<Integer>arrayList<JLabel>。 Integer将金额保存为整数,JLabel保留与字符串相同的值。我想从两个标签中随机删除元素。例如,如果20TL(t1是货币)在JLabel中删除我想要删除它在整数arrayList也。这很简单。但是接下来我想计算ArrayList中剩余金额的平均值。这是我的另一个arrayList来洗牌0到23个数字。因此,我删除了IntList和JLabel列表中的相同元素。Java如何计算某个元素随机删除的arrayList的平均值?

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方法

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:我试过用数组代替arraylist。但我无法计算出左派的平均值。因为当某些元素被移除时,数组不会缩小。

+0

你看起来过于复杂的问题。为什么不使用'JList ',用'DefaultListModel '填充它,并只处理一个集合 - 这里是列表模型。 –

+0

为了获得更好的帮助,事实上,让我们充分快速地理解您的问题的最好方法是如果您要创建并发布[最小示例程序](http:// stackoverflow。COM /帮助/ MCVE),一个小而完整的程序,只有必要的代码,以证明您的问题,我们可以复制,粘贴,编译和运行而无需修改。 –

+0

对不起,我很抱歉。我尝试它。但我看不到模型。请给我看。 –

回答

2

我会对该代码进行很多更改。首先,我会尝试创建一个值集合,这样我就不必对平行集合进行更改,因为我知道这会减少出错的几率。对于这样的事情,我会使用JList<Integer>并使用整数填充其模型,DefaultListModel<Integer>。然后,我可以使用设置为土耳其语区域设置的NumberFormat货币实例轻松地将其显示为土耳其里拉。例如,如果我创造我的模型,像这样:

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

这时如果:

// 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的单元格渲染器将其持有,这里一个整数,一个字符串表示土耳其里拉更改值后来我在一个按钮的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(); 
} 

它可能看起来,比如s ○:

enter image description here

+0

非常感谢你为你的努力。对此,我真的非常感激。 –

1

我会推荐地图:即HashMap<Integer, JLabel> money。这将确保您的两个数据集同步。

对于一般的,Java的8流是非常方便的:

Collection<Integer> amounts = money.keySet(); double average = (double) amounts.stream().mapToInt(i -> i).sum()/amounts.size()

+0

感谢您的作品太 –

+0

@ M.Aktas答案:如果他的回答帮助,通过点击向上箭头做赞成票它。 –

+0

我不能这样做。因为我没有15的声望。 –