List 转 HashMap 小算法问题..

来源:百度知道 编辑:UC知道 时间:2024/06/16 00:52:41
赶项目不想调试检查了..谁有兴趣的请看看...谢谢..
代码如下:
Long c = new Long(0);
String tempName = "";
System.out.println(tempList);
for(int i=0;i<tempList.size();i++){
if(i==0){
tempName = tempList.get(i);
}else{
if(tempName.equals(tempList.get(i))){
map.put(tempName, c);
tempName = tempList.get(i);
}else{
c =new Long(0);
tempName = tempList.get(i);
map.put(tempName, c);
}
}
c++;
}
System.out.println(map);
打印的结果:
[商铺, 商铺, 商铺, 商铺, 临铺, 临铺, 办公用房, 办公用房]
{临铺=1, 办公用房=1, 商铺=3}
我想要的结果:
[商铺, 商铺, 商铺, 商铺, 临铺, 临铺, 办公用房, 办公用房]
{临铺=2, 办公用房=2, 商铺=4}
Long c = new Long(0); 改为Long c = new Long(1); 不行..

import java.util.*;

public class Smple{

public Smple(){
ArrayList<String> tempList=new ArrayList<String>();
tempList.add("商铺");
tempList.add("商铺");
tempList.add("商铺");
tempList.add("商铺");
tempList.add("临铺");
tempList.add("临铺");
tempList.add("办公用房");
tempList.add("办公用房");
Long c = new Long(0);
HashMap<String,Long> map=new HashMap<String,Long>();
String tempName = "";
System.out.println(tempList);
for(int i=0;i<tempList.size();i++)
{
if(i==0)
{
tempName = tempList.get(i);
}
else
{
if(tempName.equals(tempList.get(i)))
{
map.put(tempName, c+1);
tempName = tempList.get(i);
}
else
{
c =new Long(0);
tempName = tempList.get(i);
map.put(tempName, c