user3942128
user3942128

Reputation:

How to add duplicate values into the ArrayList?

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

Upvotes: 2

Views: 4732

Answers (5)

Fathah Rehman P
Fathah Rehman P

Reputation: 8741

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

    }

Upvotes: 0

Naman Gala
Naman Gala

Reputation: 4692

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

Upvotes: 0

Adem
Adem

Reputation: 9429

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

Upvotes: 2

Lucas
Lucas

Reputation: 3281

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.

Upvotes: 0

Ankur Singhal
Ankur Singhal

Reputation: 26077

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

Upvotes: 2

Related Questions