列表值被新的替换

时间:2014-02-17 10:38:38

标签: java list hashmap

我正在输入

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代表Indexrange

所以例如

5.3,3.6,1.6,0.3,Iris-setosa

这应该用索引值替换。

3,2,2,0,Iris-setosa

到目前为止,我所做的是

我找到了索引,并尝试将已识别的索引添加到List中。 但是我的列表没有得到更新。我无法在列表中添加最后一个令牌 我得到了正确的索引。 代码

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 个答案:

答案 0 :(得分:1)

在for循环中,您将在行

的每次迭代中实例化List
 List<String> mapFinalList = new ArrayList<String>();

在此之后,您将值添加到列表中。因此,在每次迭代时,都会创建一个新列表,并丢失上一次迭代的值。新列表将包含当前循环中插入的值。因此列表仅包含当前值,最后一个值丢失。您需要做的是,在for循环之外创建一个列表并仅将列表实例化一次,并对所有迭代使用相同的实例。这样,这些值将一个接一个地添加,并且不会被替换。

相关问题