简体   繁体   English

检查并在数组列表中添加浮点值

[英]Check and add float values in arraylist

Hi I am new to java and am trying to understand the arraylist. 嗨,我是Java的新手,正在尝试了解arraylist。 I am using Arraylist theList and dummyValues . 我使用的ArrayList 的thelistdummyValues。 I read the values from theList and update the float values in dummyValues . 我读的thelist值和dummyValues更新的浮点值。 My code snippet is as follows ` 我的代码段如下

     public static void generateValues(ArrayList<Float> theList) {

     for (int j = 0; j < theList.size(); j++) {
         if (dummyValues.size()==0)
                 dummyValues.add(j, theList.get(j));

          else 
                dummyValues.set(j, theList.get(j));
                   }     
}

I trying add the values to the ArrayList dummyValues in the first condition and in the second condition if the size dummyValues is greater than 0 just update the values of dummyValues . 我尝试在第一个条件中将值添加到ArrayList dummyValues中,在第二个条件中,如果大小dummyValues大于0,则只需更新dummyValues的值即可 I have considered this method to avoid duplicate copies. 我已经考虑过这种方法以避免重复副本。

But, when I execute it I get the following error : 但是,当我执行它时,出现以下错误:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 java.lang.IndexOutOfBoundsException:无效的索引1,大小为1

The error occurs here dummyValues.set(j, theList.get(j)); 错误发生在这里dummyValues.set(j, theList.get(j));

I know this is a trivial concept, any kind of help is appreciated 我知道这是一个琐碎的概念,任何帮助都值得赞赏

I would suggest this improvement. 我建议这种改进。 Change your method to this: 将您的方法更改为此:

public static void generateValues(ArrayList<Float> theList) {

 for (int j = 0; j < theList.size(); j++) {
     if (dummyValues.size()==0){
             dummyValues.add(j, theList.get(j));
       }
      else if(dummyValues.size()==theList.size()){
            dummyValues.set(j, theList.get(j));
               }     
     else{
         dummyValues.add(theList.get(j));
    }
}

Check for size of both lists, if lists aren't same size it will add new element to dummy list instead of try to set element on nonexisting index. 检查两个列表的大小,如果列表大小不同,它将向虚拟列表添加新元素,而不是尝试在不存在的索引上设置元素。 In this case its possible that lists wont have same order. 在这种情况下,列表可能不会具有相同的顺序。

EDIT: This is not right answer!!! 编辑:这不是正确的答案!!! Sorry I'm writting faster than thinking. 抱歉,我的写作速度比思考的快。 :( It works only if theList.size() > dummyValues.size() . I'll try to imprvove my answer ;) :(仅当theList.size() > dummyValues.size() 。我会尽量提高回答的准确性;)

EDIT2: Hello again. EDIT2:再次您好。 Did some work and I'm back. 做了一些工作,我回来了。 I reworked your method and have second, in my opinion better, solution for you. 我重新设计了您的方法,并以我认为更好的方法为您提供了第二个解决方案。 Check this: 检查一下:

public static void generateValues(List<Float> theList) {

  if (dummyValues.size() >= theList.size()) { 
    for (float value : theList) {
      dummyValues.set(theList.indexOf(value), value);
    }
  }
  else {
    for (float value : theList) {
      dummyValues.add(dummyValues.size(), value);
    }
  }
}

Try it out and let me know if it fits your needs. 尝试一下,让我知道它是否适合您的需求。

Consider that if dummyList is empty, when j is 0 it adds one element from theList to it, but when j is 1 you are trying to set the element on position 1 from dummyList . 想想看,如果dummyList是空的,当j为0它增加了一种元素的thelist到它,但是当j是1,你正试图从dummyList设置位置1的元素。 But at that moment, dummyList only has one element, which is on position 0. This is why you're getting the error. 但是在那一刻, dummyList只有一个元素,位于位置0。这就是为什么会出现错误的原因。

One solution would be to ensure that you create dummyList with the initial capacity (or size if you prefer) as theList 's size. 一种解决方案是确保您创建的dummyList的初始容量(或大小,如果您愿意)为List的大小。

 public static void generateValues(ArrayList<Float> theList) {

 for (int j = 0; j < theList.size(); j++) {
     if (dummyValues.size()==0){
             dummyValues = new ArrayList<>(theList.size());
             dummyValues.add(j, theList.get(j));

      } else 
            dummyValues.set(j, theList.get(j));
   }     

} }

You haven't accounted for the fact that if dummyValues only has 1 element in it, it will pass dummyValues.size()==0) but will only have an index of 0 since ArrayList begins counting at 0. So if we're at index 1 of theList it will throw you the exception in question. 您没有考虑以下事实:如果dummyValues仅包含1个元素,它将传递dummyValues.size()==0)但由于ArrayList从0开始计数,因此其索引将为0。因此,如果在指数1 theList它会抛出你的问题除外。

You may have to add another condition for dummyValues.size()==1) that's all :) 您可能必须为dummyValues.size()==1)添加另一个条件:)

  • Make sure the variables and initialised. 确保变量已初始化。

     dummyValues = new ArrayList<Float> (); 
  • Then you can do this checking before doing anything in code - 然后,您可以在执行代码中的任何操作之前进行此检查-

      public void generateValues(ArrayList<Float> theList) { if (theList.size() > 0) { for (int j = 0; j < theList.size(); j++) { if (dummyValues.size() == 0) { dummyValues.add(theList.get(j)); } else { try { if (dummyValues.get(j) != null) { dummyValues.set(j, theList.get(j)); } } catch (IndexOutOfBoundsException e) { //no element present here . } } } } } 

This will check if theList has any value at all then start the loop. 这将检查theList是否有任何值,然后开始循环。 if the dummyValues is empty then add the 0th element or go on replacing the dummyValues with the theList values. 如果dummyValues为空,则添加第0个元素,或者继续用theList值替换dummyValues。

Give this a try and let me know if it works for you. 试试看,让我知道它是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM