2014-03-27 15 views
1

你好的朋友之一ArrayList中獲得獨立的數據項值我有一個自定義的數組列表,其中包括類的物品:[t_id(PK) cat_id(FK), cat_name,total] .如何從Android中

想我就可以輸入5倍的值一樣({1,1,c1,100},{2,1,c1,200},{3,1,c1,300},{4,1,c2,400},{5,1,c2,500})

現在我想總價值"c1"類別和"c2"

手段輸出的應該是這樣

"c1=600" and "c2=900". 

所以任何想法如何我可以解決這個問題

+0

它是自定義對象的數組列表嗎? – playmaker420

+0

@ playmaker420自定義對象 –

+0

爲自定義對象設置獲取者和設置者。並迭代你的數組列表。對於(int i = 0; i playmaker420

回答

2

我不知道豆怎麼了,在這裏我想的東西,可以幫助你,

 Map<String, Integer> map = new HashMap<String, Integer>(); 
     for(Custom custom : customList) 
     { 
      if(map.containsKey(custom.getCat_name())) 
      { 
       Integer i = map.get(custom.getCat_name()); 
       map.put(custom.getCat_name(), i+custom.getTotal()); 
      } 
      else 
      { 
       map.put(custom.getCat_name(), custom.getTotal()); 
      } 
     } 

     System.out.println("Map : "+map); 

輸出:Map : {c1=600, c2=900}

+0

感謝發佈它正在爲我工​​作 –

+0

@HarshalKalavadiya您隨時歡迎。 – newuser

1

這是猜測,但我的理解就是這樣

public class Custom 
{ 
int t_id(PK); 
int cat_id(FK); 
String cat_name; 
int total; 

public Custom(int t_id(PK),int cat_id(FK),String cat_name,int total) 
{ 
this.t_id(PK)=t_id; 
this.cat_id(FK)=cat_id(FK); 
this.cat_name=cat_name; 
this.total=total; 
} 
} 

ArrayList<Custom> list=new ArrayList<Custom>; 

現在,如果你想獲得總不爲c1

這樣做。

int totalC1=0; 
for(Custom c:list) 
{ 
if(c.cat_name.equals("c1")) 
{ 
totalC1+=c.total; 
} 
}