【问题标题】:List value getting replaced with new ones列表值被新值替换
【发布时间】:2014-02-17 10:38:38
【问题描述】:

我有一个输入

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<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

请提出建议。

【问题讨论】:

  • 您的意思是每次添加索引时您的值都会被替换?

标签: java list hashmap


【解决方案1】:

在你的 for 循环中,你在行的每次迭代中都实例化 List

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

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

【讨论】:

  • 那上面那一行的if(mapFinalList == null) {呢?
  • 我指的是外部 for 循环中的 mapFinalList = new ArrayList&lt;String&gt;();。我已经编辑了代码以显示完整的行。
  • 哦,你是对的!由于缩进不佳,我根本没有注意到其他两层循环......
  • 我正在尝试将每行的索引值作为每个列表获取
  • @UnmeshaSreeVeni 对不起.. 我不明白那个评论。你能解释一下你到底需要什么吗?
猜你喜欢
  • 2021-09-07
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
  • 2019-07-28
  • 2016-10-26
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多