[英]Java mapping data from two arrays and insert into third array
我很難將兩個列表中的數據映射到第三個列表中。 我的樣本數據如下:
Categorylist ID: 1,2,3,4,5,6,7,8,9,10,42,46,49,50
CurrentMonthByCat ID: 1,2,3,4,5,6,7,8,9,10,42,49,50
(the transaction amount value for CurrentMonthByCat: 92,46,40,180,60,10,1510,200,500,10,234,12)
currentMonthByCat中缺少46個。 我正在嘗試以某種方式做到這一點,如果在類別列表ID中不存在currentMonthByCat ID,我將在第三個列表中插入0,而不是從CurrentMonthByCat獲取交易金額並將其推入第三個列表。
ArrayList<Double> total = new ArrayList<Double>();
for(int i = 0; i < categorylist.size(); i++){
for(int j = 0; j < currentMonthByCat.size(); j++){
if(categorylist.get(i).getCategoryID().equals(currentMonthByCat.get(j).getCategory().getCategoryID())){
Log.d("IIIII", categorylist.get(i).getCategoryID());
Log.d("JJJJJ", currentMonthByCat.get(j).getCategory().getCategoryID());
total.add((double)currentMonthByCat.get(j).getTransactionAmt());
}else{
total.add(0.0);
}
}
}
for(int k = 0; k < total.size(); k++){
Log.d("KKKKK", String.valueOf(total.get(k)));
}
但是總列表的打印結果是:
92,0,0,0,0,0,0,0,0,0,0,0,0,0,46,0,0,0...
我所期望的是:
92,46,40,180,60,10,1510,200,500,10,0,234,12
我只想在currentMonthByCat中的ID與categorylist中的ID不匹配時插入0。 例如,ID 46,它是從右數第3個位置。
我意識到原因是因為首先我在第三個數組中插入了92,然后類別列表ID仍為1,然后它將與currentMonthByCat中的所有其余內容進行比較,然后再移至ID2。這就是為什么不必要的零。 但是我不確定如何對它進行排序以實現我想要的。
有任何想法嗎?
提前致謝。
將您的值放入Map<Integer, Double>
Map<Integer, Double> map = new HashMap<Integer, Double>();
for (int i = 0; i < currentMonthByCat.size(); ++i) {
//... categoryId = currentMonthByCat.get(i).categoryId
//... amount = currentMonthByCat.get(i).amount
map.put(categoryId, amount);
}
然后使用Categorylist ID中的值遍歷地圖:
// create result arraylist
ArrayList<Double> total = new ArrayList<Double>();
for (int i = 0; i < categorylist.size(); ++i) {
Double amount = map.get(categorylist.get(i));
if (amount == null) {
total.add(0.0);
} else {
total.add(amount);
}
}
結果列表total
將包含現有映射的數量,或不包含零的數量。
其他方法如果可以保證categorylist被排序並且CurrentMonthByCat被排序
然后,您可以遍歷列表中的一個,同時保持索引/光標到另一個列表,而不是從頭開始迭代另一個列表,而是從以前記住的游標值開始迭代,從而獲得比n ^ 2更好的平均性能
這很簡單。 除非內部循環完成,否則您無法決定在總數組中添加零或值。 所以可能您添加了元素existAtIndex並用-1對其進行了初始化,如果找到該元素,然后將索引分配給existAtIndex並中斷了循環,或者如果不存在則添加零。 因此代碼將類似於:
ArrayList<Double> total = new ArrayList<Double>();
int existAtIndex;
for(int i = 0; i < categorylist.size(); i++){
// search for the element index
existAtIndex = -1;
for(int j = 0; j < currentMonthByCat.size(); j++){
if(categorylist.get(i).getCategoryID().equals(currentMonthByCat.get(j).getCategory().getCategoryID())){
existAtIndex = j;
break;
}
}
// add the value in the element index or add zero if the element not exist
if (existAtIndex != -1) {
total.add((double)currentMonthByCat.get(existAtIndex).getTransactionAmt());
}
else {
total.add(0.0);
}
}
for(int k = 0; k < total.size(); k++){
Log.d(String.valueOf(total.get(k)));
}
為了獲得更好的代碼,您可以使用contains方法檢查是否在arrayList中存在該項目,而不是使用基本循環。 祝好運
您在此處嘗試執行的操作有很多代碼。 我認為以下片段以一種易於閱讀和可維護的方式滿足您的需求。
//First of all we are interested in getting a transaction amount for each value in currentMonthByCat
//so loop around using your object (not sure what it's called)
for(CurrentMonth value : currentMonthByCat){
//check if it's present.
//We create a new method here that gets you your category list as a list of integers.
//This is key to making the whole method much more readable.
if(categorylist.getIdsAsList().contains(value.getCategory().getCategoryID())){
//it is so add it
total.add(value.getTransactionAmt());
} else {
//it's not so add a 0
total.add(0.0);
}
}
getIdsAsList方法如下所示:
public List<Integer> getIdsAsList(){
List<Integer> result = new ArrayList<>();
for (CategoryListItem item : categorylist) {
result.add(item.getCategoryId());
}
return result;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.