-1

Im trying to loop though an array list and add or remove items from it, its a question from Codegym.cc, im not sure what the solution is, any help? i had it sort of working before i added the last 2 if statements, but i really havent a clue what im doing wrong.

public class Cat {
    public static void main(String[] args) throws Exception {
        ArrayList<String> list = new ArrayList<String>();
    list.add("rose"); 
   list.add("measure"); 
    list.add("love"); 
    list.add("lyre"); 
    list.add("wade");
    list.add("bark");         

    list = fix(list);

    for (String s : list) {
        System.out.println(s);
    }
}

public static ArrayList<String> fix(ArrayList<String> list) {
    // write your code here     
        //loop through array
    for (int i = 0; i < list.size();){
        //if contains r and not l -remove it
        if(list.get(i).contains("r") && !list.get(i).contains("l")){

            list.remove(i);
            if(i > 0) {i--;}    
        }
        //if contains l and not r - dub entry 
        if(!list.get(i).contains("r") && list.get(i).contains("l")){

            list.add(i, list.get(i));
            if(i == list.size()-1) {break;}else {i+=2;}
        }
        //contains none - do nothing
        if(!list.get(i).contains("r") && !list.get(i).contains("l")){
            i++;
        }
        //contains both - do nothing
        if(list.get(i).contains("r") && list.get(i).contains("l")){
            i++;
        }

      }



    return list;

 }
}
Derek Pollard
  • 6,953
  • 6
  • 39
  • 59
john
  • 65
  • 1
  • 9
  • one suggestion for cleaner code is at the beginning of your loop, do String entry = list.get(i); and just reference "entry" throughout your loop.... that way you won't be calling list.get(i) a million times – RobOhRob May 01 '19 at 20:52

2 Answers2

1

Since you are removing elements from the lists, the size of the list decrements, and you are running the loop till the size of list (which has infact changed), that's why OutOfIndexException :)

user699848
  • 109
  • 7
1

There is a point that you should notice. When manipulating arrays, you should mention what to do when you reach the last element. In CS, array counting starts at 0. In an array of length 4 the last element is of index 3 and not 4.

The problem is you are not checking this case your last two if cases where an increment is made without boundary check. This means when you would come back to the first if statement there is is a chance of exception.

The other problem, is the for loop will never end.

This is a quick solution to remove the exception. You must notice that the program will not stop. You should specify some condition to get out of the for loop.

Note: You will end up having a gigantic array with the word love alone.

public static ArrayList<String> fix(ArrayList<String> list) {
    // write your code here     
        //loop through array
    for (int i = 0; i < list.size();){
        //if contains r and not l -remove it
        if(list.get(i).contains("r") && !list.get(i).contains("l")){
                System.out.println(list.toString());
            list.remove(i);
          System.out.println(list.toString());
            if(i > 0) {i--;}    
        }
        //if contains l and not r - dub entry 
        if(!list.get(i).contains("r") && list.get(i).contains("l")){

          System.out.println(list.toString());
            list.add(i, list.get(i));
          System.out.println(list.toString());
            if(i == list.size()-1) {break;}else {i+=2;}
        }
        //contains none - do nothing
        if(!list.get(i).contains("r") && !list.get(i).contains("l")){

        }
        //contains both - do nothing
        if(list.get(i).contains("r") && list.get(i).contains("l")){

        }

      }



    return list;

 }
Nesan Mano
  • 1,892
  • 2
  • 26
  • 43