0

Hi I am new to java and am trying to understand the arraylist. I am using Arraylist theList and dummyValues. I read the values from theList and update the float values in 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. 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

The error occurs here dummyValues.set(j, theList.get(j));

I know this is a trivial concept, any kind of help is appreciated

4
  • 2
    Are you sure that dummValues and theList do have the same size? otherwise you might be ignoring values or you get your exception. In this particular case it seems that theList is greater then dummyList. Commented Apr 7, 2016 at 13:42
  • dummyValues has only one element .but you call set for index 1 and etc...since there is only one element you can only set for index 0 Commented Apr 7, 2016 at 13:47
  • @FastSnail thanks for the information. Now I understand you can only set on indexes which have elements in them. Is there method to avoid duplication of elements? Commented Apr 7, 2016 at 14:00
  • @ADI there is better collection types for that outher than arraylist Commented Apr 7, 2016 at 14:09

4 Answers 4

2

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

EDIT2: Hello again. 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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. But at that moment, dummyList only has one element, which is on position 0. This is why you're getting the error.

One solution would be to ensure that you create dummyList with the initial capacity (or size if you prefer) as theList's size.

 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));
   }     

}

Comments

0

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.

You may have to add another condition for dummyValues.size()==1) that's all :)

Comments

0
  • 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. if the dummyValues is empty then add the 0th element or go on replacing the dummyValues with the theList values.

Give this a try and let me know if it works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.