[英]Java how to calculate average of an arrayList that some elements removed randomly?
我有一個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);
}
這是我刪除的style.this在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個元素。 我希望我能很好地說明我的問題。
附:我已經嘗試使用數組而不是arraylist。 但是我無法計算左手的平均值。 因為刪除某些元素時數組不會收縮。
我將對該代碼進行很多更改。 首先,我將嘗試創建一個值集合,這樣我就不必費心對並行集合進行更改,因為我知道這會減少出錯的機會。 對於這樣的事情,我將使用JList<Integer>
並使用JList<Integer>
填充其模型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的單元格渲染器會將其持有的值(這里是Integer)更改為土耳其里拉的String表示形式:
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();
}
它可能看起來像這樣:
我會推薦一個Map:即HashMap<Integer, JLabel> money
。 這將確保您的兩個數據集是同步的。
一般而言,Java 8流確實很方便:
Collection<Integer> amounts = money.keySet(); double average = (double) amounts.stream().mapToInt(i -> i).sum() / amounts.size()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.