Manahil
Manahil

Reputation: 265

Swapping string position in Arraylist java

I have a sentence: Humpty Dumpty sat on a wall. I want the strings to swap positions such that : Dumpty Humpty on sat wall a.

So the code that I wrote is following :

import java.util.*;
public class Swap{
public static void main(String []args) {
    ArrayList<String> sentence = new ArrayList<String>();
    sentence.add("Humpty");
    sentence.add("Dumpty");
    sentence.add("sat");
    sentence.add("on");
    sentence.add("a");
    sentence.add("wall");

    int size = sentence.size() ; // for finding size of array list
    int numb ; 

    if(size%2 == 0) {
        numb = 1;
    }
    else {
        numb = 0;
    }

    ArrayList<String> newSentence = new ArrayList<String>();

    if(numb == 1) {
        for(int i = 0; i <= size ; i = i+2) {
            String item = sentence.get(i);
            newSentence.add(i+1, item);
        }

        for(int i =  1; i<=size ; i = i+2) {
            String item2 = sentence.get(i);
            newSentence.add(i-1, item2);
        }


        System.out.println(newSentence);
    }

    else {
        System.out.println(sentence);
    }

    }
}

The code is compiling correct but when I run it, its giving an error. What i understand of this is that I am adding strings to the array list leaving positions in between. Like adding at position 3 without filling position 2 first. How do I overcome this problem ?

Upvotes: 0

Views: 96

Answers (3)

Estimate
Estimate

Reputation: 1461

That is because:

for(int i = 0; i <= size ; i = i+2) {
            String item = sentence.get(i);
            newSentence.add(i+1, item);//Here you will face java.lang.IndexOutOfBoundsException
        }

        for(int i =  1; i<=size ; i = i+2) {
            String item2 = sentence.get(i);
            newSentence.add(i-1, item2);//Here you will face java.lang.IndexOutOfBoundsException
        }

instead of this, try following code:

if(numb == 1) {
         for(int i = 0; i < size-1 ; i +=2) {
              Collections.swap(sentence, i, i+1);
         }

}

Upvotes: 0

Saif
Saif

Reputation: 7042

You can initilize newSentence using:

ArrayList<String> newSentence = new ArrayList<String>(Collections.nCopies(size, ""));

This will let you access/skip any position in between 0 and size. So you can keep your rest of the code as it is.

just remember all index are being populated with empty String here.

Upvotes: 0

Ori Lentz
Ori Lentz

Reputation: 3688

You're correct about your problem - you're trying to insert an element into index 1 before inserting an element at all (at index 0), and you get an IndexOutOfBoundsException.

If you want to use your existing code to achieve this task, simply have just one loop as such:

if(numb == 1) {
    for(int i = 0; i < size-1 ; i = i+2) {
        String item = sentence.get(i+1);
        newSentence.add(i, item);
        item = sentence.get(i);
        newSentence.add(i+1, item);
    }
}

If you want to be a bit more sophisticated a use Java's built-in functions, you can use swap:

for(int i = 0; i < size-1 ; i = i+2) {
    Collections.swap(sentence, i, i+1);
}

System.out.println(sentence);

Upvotes: 2

Related Questions