2014-02-17 70 views
1

我有一个输入列表值越来越换成新的

5.3,3.6,1.6,0.3,Iris-setosa 
4.9,3.3,1.6,0.3,Iris-setosa 
4.9,3.3,1.3,0.3,Iris-setosa 
4.6,3.3,1.6,0.3,Iris-setosa 
5.3,3.6,1.6,0.3,Iris-setosa 

,我有一个列表

BinList {3=[index=0 {from=1.3,to=1.42}, index=1 {from=1.42,to=1.5399999999999998}, index=2 {from=1.5399999999999998,to=1.6599999999999997}, index=3 {from=1.6599999999999997,to=1.7799999999999996}, index=4 {from=1.7799999999999996,to=1.8999999999999995}, index=5 {from=1.8999999999999995,to=1.9}], 2=[index=0 {from=2.9,to=3.2399999999999998}, index=1 {from=3.2399999999999998,to=3.5799999999999996}, index=2 {from=3.5799999999999996,to=3.9199999999999995}, index=3 {from=3.9199999999999995,to=4.26}, index=4 {from=4.26,to=4.6}, index=5 {from=4.6,to=4.6}], 1=[index=0 {from=4.3,to=4.62}, index=1 {from=4.62,to=4.94}, index=2 {from=4.94,to=5.260000000000001}, index=3 {from=5.260000000000001,to=5.580000000000001}, index=4 {from=5.580000000000001,to=5.9}], 4=[index=0 {from=0.3,to=0.36}, index=1 {from=0.36,to=0.42}, index=2 {from=0.42,to=0.48}, index=3 {from=0.48,to=0.54}, index=4 {from=0.54,to=0.6}]} 

其中,例如

3=[index=0 {from=1.3,to=1.42}, index=1 {from=1.42,to=1.5399999999999998}, index=2 {from=1.5399999999999998,to=1.6599999999999997}, index=3 {from=1.6599999999999997,to=1.7799999999999996}, index=4 {from=1.7799999999999996,to=1.8999999999999995}, index=5 {from=1.8999999999999995,to=1.9}] 

key 3代表column numbervalue代表Indexa nd range

所以例如

5.3,3.6,1.6,0.3,Iris-setosa 

这应该与索引值来替换。

3,2,2,0,Iris-setosa 

到目前为止,我所做的是

我发现索引并试图识别出的索引添加到列表中。 但我的列表没有得到更新。我无法在列表中追加最后一个标记 我得到正确的索引。 代码

List<String> mapList = new ArrayList<String>(); 
Map<String, List<Attribute>> binList = new HashMap<String, List<Attribute>>(); 

//mapList contains the input value 
for (String temp : mapList) { 
System.out.println("temp: "+temp); 

//Setting column number for inputdata 
int indexMap=1; 
StringTokenizer st = new StringTokenizer(temp,","); 
while(st.hasMoreTokens()){ 
String token = st.nextToken(); 
System.out.println("token()"+token); 

//Checking to find same column number in list 
List<String> mapFinalList = new ArrayList<String>(); 
for (Map.Entry<String, List<Attribute>> entry : binList.entrySet()) 
{ 

    //Key similarity 
if(entry.getKey().equals(String.valueOf(indexMap))){ 

    System.out.println("equal"+entry.getKey()+"=="+indexMap); 

    //Iterating through the values of found out key 
    for (Attribute a : entry.getValue()) 
     { 

      //Parsing the values 
      ParseValue.parse(a).toString(); 
      int index = ParseValue.getIndex(); 
      double rangeFrom = ParseValue.getrangeFrom(); 
      double rangeTo = ParseValue.getrangeTo(); 


      // Condition Checking for bins 
      if((Double.parseDouble(token) <= rangeTo)&& (Double.parseDouble(token) < rangeTo)){ 
       System.out.println("replace: "+index); 
       if(mapFinalList == null) { 
       mapFinalList = new ArrayList<String>(); 
       } 

       mapFinalList.add(String.valueOf(index)); 
       System.out.println("List "+mapFinalList); 
       break; 

       }//if loop 

      }//for loop 
      }//if loop 

     }//for loop 

     indexMap++; 

     }//while 

    }//for 

输出

In BinningInput 
5.3,3.6,1.6,0.3,Iris-setosa 
temp: 5.3,3.6,1.6,0.3,Iris-setosa 
token()5.3 
equal1==1 
replace: 3 
List [3] 
token()3.6 
equal2==2 
replace: 2 
List [2] 
token()1.6 
equal3==3 
replace: 2 
List [2] 
token()0.3 
equal4==4 
replace: 0 
List [0] 
token()Iris-setosa 

请建议。

+1

你的意思是说你的价值观在每次添加索引时都会被取代? – Adarsh

回答

1

在你的循环,你在该行

List<String> mapFinalList = new ArrayList<String>(); 

实例在每次迭代的列表之后,你自己的价值添加到列表中。所以在每次迭代中,都会创建一个新列表,并且最后一次迭代的值将丢失。新列表将包含当前循环中插入的值。所以这个列表只包含当前值,最后一个值丢失。你需要做的是,在for循环之外创建一个列表,并且仅对该列表实例化一次,并对所有迭代使用相同的实例。这样,这些值就相继添加,不会被替换。

+0

那么if(mapFinalList == null){'在上面那行? –

+0

我指的是'mapFinalList = new ArrayList ();'在外部for循环中。我编辑了代码以显示完整的行。 – Adarsh

+0

哦,你是对的!我没有注意到那些其他两层循环,由于可怜的缩进...... –