2

I have to edit the list of String:

  1. Delete the value if it contains "h" letter;
  2. Do nothing if it contains "h" and "o"
  3. Add to the list duplicate of the value if it contains letter "o".

I stuck on a step of making a duplicate. What is wrong? And why occurs?

Here is the code:

public class MyClass {
    public static void main(String[] args) throws Exception {
        ArrayList<String> list = new ArrayList<String>();
        list.add("hi");      // this one have to be removed
        list.add("hello");   // this one have to be left without changes
        list.add("ops");     // duplicate must be added to the list
        list = fix(list);

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

    public static ArrayList<String> fix(ArrayList<String> list) {

        for (int i = 0; i < list.size(); i++) {

            if (list.get(i).contains("h") && list.get(i).contains("o")) ;
                // do nothing

            else if (list.get(i).contains("h"))
                list.remove(i);

            else if (list.get(i).contains("o")) {
                // The problem is here ===>
                list.add(i + 1, list.get(i));

            }
        }

        return list;
    }

}

I also tried:

String s = new String(list.get(i));
list.add(i+1, s);
3
  • 1
    I suggest you ask the person who gave you this homework. It could mean any number of things. Commented Dec 15, 2014 at 11:02
  • Which is the problem? Any Exception? Commented Dec 15, 2014 at 11:03
  • You mean it keeps adding to the pointer position? Why not: list.add(i, list.get(i++));? Commented Dec 15, 2014 at 11:04

5 Answers 5

2

you are adding an element, and your are checking it again and add one more element again. this creates a infinite loop. this will fix your problem. moving checking index one forward to prevent this infinite loop

else if (list.get(i).contains("h"))
{
  list.remove(i);
  i--;
}
else if (list.get(i).contains("o"))
{
  list.add(i+1, list.get(i));
  i++;
}

moreover, you need to decrease i when you remove an element

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

Comments

2

You should be getting OOM, Out Of Memory Error.

Since you are adding same object which is at ith location to i+1 location also, so it will keep on adding it recursively.

Code Fix Need to increment i in the last else block.

change

else if (list.get(i).contains("o")) {
                // The problem is here ===>
                list.add(i + 1, list.get(i));

            }

TO

  else if (list.get(i).contains("o")) {
                // The problem is here ===>
                list.add(i + 1, list.get(i));
                i++;
            }

1 Comment

@Leonid i have provided the fix, if it solve your purpose, please accept/upvote
0

You cannot modify the size of the List inside of for-each loop. You need to use Iterator for that:

Iterator<String> iterator = list.iterator();

and then

while (iterator.hasNext()) {
   String someString = iterator.next();
   //your code
   list.add(someString); //if you need to do that
} 

you create a referene to current object by calling next() method.

Comments

0

Create an new list object and add the value in that, and after for loop you can add all elements of new list in the original list to avoid infinite loop (as you are iterating over list size which is getting changed when you are adding a new element).

Create a temporary list

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

change below code

else if (list.get(i).contains("o")) {
    list.add(i+1, list.get(i));
}

To this code

else if (list.get(i).contains("o")) {
    newList.add(list.get(i));
}

After for loop append the newList to original list.

list.addAll(newList);

Comments

0
 public class MyClass
        {
            public static void main(String[] args) throws Exception
            {
                ArrayList<String> list = new ArrayList<String>();
                list.add("hi");      // this one have to be removed
                list.add("hello");   // this one have to be left without changes
                list.add("ops");     // duplicate must be added to the list
                list = fix(list);

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

            public static ArrayList<String> fix(ArrayList<String> list) {

                ArrayList<String>tempList=new ArrayList<>();
                for (int i = 0; i < list.size(); i++){

                    if (list.get(i).contains("h") && list.get(i).contains("o"))
                    {
                      tempList.add(list.get(i));
                    }                     
                    else if (list.get(i).contains("o"))
                    {
                       tempList.add(list.get(i));
                       tempList.add(list.get(i));

                    }
                }

                return tempList;
            }

    }

Comments

Your Answer

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