亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 foreach 將 arrayList 與自身進行比較,不包括重復項

使用 foreach 將 arrayList 與自身進行比較,不包括重復項

守著一只汪 2022-10-20 17:18:45
我正在使用 foreach 將 arrayList 與自身進行比較。我有一個包含服務員提示的arrayList,每個對象都有一個日期“dd-MM-yyy”和一個金額(雙倍),現在我想添加同一天的所有交易,所以我可以得到當天的總數在服務員之間分配。沒有重復。我已經特別在這里查看了所有內容,但我似乎無法找到解決方案。我真的希望你們能幫忙,我知道這有點尷尬,因為問題很簡單,但我已經研究了幾天了,我被卡住了。我有一個更長的算法,但它不起作用,我在網上找不到任何解決方案,所以我把它分解成最基本的組件并檢查每個步驟,很早就出現了這個問題:我正在使用本地數組列表,以確保我不會一遍又一遍地比較同一天。if(!alreadyMade.contains(tips1.getTime()) 后跟 alreadyMade.add(tips1.getTime()) 似乎正在產生重復,在我看來這沒有任何意義。我想要的只是添加所有交易同一天從同一個arrayList。公共無效dist(){    double day = 0;    List<String> alreadyMade = new ArrayList<>();    for (Tips tips : data.getTips()) {        for (Tips tips1 : data.getTips()) {            if(tips.getTime().equals(tips1.getTime())) {                if (!alreadyMade.contains(tips1.getTime())){                    alreadyMade.add(tips1.getTime());                    day += tips.getTips();                }            }        }        System.out.println(day);        day = 0;    }}我希望打印一天,但打印了很多沒有意義的數字
查看完整描述

3 回答

?
DIEA

TA貢獻1820條經驗 獲得超3個贊

我認為你正在嘗試做這樣的事情:


        Map<String,Double> alreadyMade = new HashMap<>();

    for (Tips tips : new ArrayList<Tips>()) {

        // If this time doesn't exist in the map then add it to the map with the

        // value tips.getTips().  If this time does exist in the map then add 

        // the value of tips.getTips() to the value that is already in the map.

        alreadyMade.merge(tips.getTime(), tips.getTips(), (Double a, Double b) -> a + b);

    }

    // go through each map entry.  The keys are the times and the values are the tip totals for that time.

    for (Map.Entry<String, Double> entry : alreadyMade.entrySet()) {

        System.out.println("Time: " + entry.getKey() + " Tips: " + entry.getValue());

    }

注意:我無法對此進行測試,因為我正在運行 Java 7,并且此地圖功能在 Java 8 之前不可用。


查看完整回答
反對 回復 2022-10-20
?
慕田峪9158850

TA貢獻1794條經驗 獲得超8個贊

在 Java 8+ 中,您可以使用流 API 按時間分組:

Map<Date, Integer> alreadyMade = data.getTips().stream()
  .collect(groupingBy(Tip::getTime, summingInt(Tip::getTips)));


查看完整回答
反對 回復 2022-10-20
?
www說

TA貢獻1775條經驗 獲得超8個贊

我會這樣做:


這是你的提示課(我認為)


public class Tip{

    Date date;

    float tip;

    public Tip(Date date, float tip){

        this.date = date;

        this.tip = tip;

    }

}

這就是(“算法”)


//To Format the Dates

SimpleDateFormat ft = new SimpleDateFormat("dd-MM-yyyy");


//Input

ArrayList<Tip> tips = new ArrayList<Tip>();


//Just some Data for testing

tips.add(new Tip(ft.parse("11-04-2019"), 2.40F));

tips.add(new Tip(ft.parse("25-04-2019"), 3.30F));

tips.add(new Tip(ft.parse("25-04-2019"), 0.90F));




//Output

ArrayList<Date> dates = new ArrayList<Date>();

ArrayList<Float> sum = new ArrayList<Float>();


for(Tip tip : tips){  //Go through each Tip

  int match = dates.indexOf(tip.date);  //Look if the date is already in the array (if not -> -1)


  if(match == -1){  //If not add it

    dates.add(tip.date);

    sum.add(tip.tip);

  }else {  //If yes set it

    sum.set(match, sum.get(match) + tip.tip);

  }


}


//Output to console

for(int i = 0; i < dates.size(); i++){

  System.out.println(ft.format(dates.get(i)).toString() + " " + String.valueOf(sum.get(i)));

}

還有一個帶有地圖或配對的解決方案,但我從未使用過它們(不是專業編碼器)。還要確保嘗試捕獲 ParseException。我希望這就是你的意思。:)


查看完整回答
反對 回復 2022-10-20
  • 3 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號