1

I am trying to figure out how to add a string, into a string ArrayList, between two strings that are already in. So if I have this

ArrayList<String> List = new ArrayList<String>();
List.add("Yes");
List.add("No");
List.add("Maybe");

How would I go along putting the word "Or" between them and make the ArrayList contain "Yes" "Or" "No" "Or" "Maybe"?

1
  • Yes, but will it push the string already in that index forward one, along with all the ones ahead of it? Commented Mar 27, 2014 at 18:00

2 Answers 2

2

I have three advices.

First, to name the variables, start with lower-case.

Second, use List as type of variable, instead of ArrayList, you will thank me later, trust me.

Third, to do what you ask for, there is overloaded method add for choosing position :

    List<String> list = new ArrayList<String>();
    list.add("Yes");
    list.add("No");
    list.add(1,"Maybe"); //insert into position 1 and shift everything to the right.

For this example, if you use System.out.println(list);, you will get this output :

[Yes, Maybe, No]

For adding Or instruction, it would be like this :

    List<String> list = new ArrayList<String>();
    list.add("Yes");
    list.add("No");
    list.add("Maybe");
    list.add(1, "Or");
    list.add(3, "Or");
    System.out.println(list);

Output :

[Yes, Or, No, Or, Maybe]

Also, if you want to make your program more re-usable, you can write a method, that will do this for you for any case of list :

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    list.add("Yes");
    list.add("No");
    list.add("Maybe");
    list.add("Probably");
    list.add("Never");

    List<String> orList = addOr(list);
    System.out.println(orList);
}

public static List<String> addOr(List<String> list){
    List<String> newList = new ArrayList<>();
    int count = 0;
    for(String text : list){
        count++;
        newList.add(text);
        if (count != list.size()){
            newList.add("Or");
        }
    }
    return newList;
}

Having this output :

[Yes, Or, No, Or, Maybe, Or, Probably, Or, Never]

However, if you want to use that list for outputing some message for user, it is not good idea to add "Or", because it is really not part of information. Rather it is good, to create method, which will create output String you desire.

This code

public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    list.add("Yes");
    list.add("No");
    list.add("Maybe");
    list.add("Probably");
    list.add("Never");

    String niceOutput = addOr(list);
    System.out.println("Choose from following options: " + niceOutput);
}   

public static String addOr(List<String> list){
    String orText = "";
    int count = 0;
    for(String text : list){
        count++;
        orText += '\'' + text + '\'';
        if (count != list.size()){
            orText += " or ";
        }
    }
    return orText;        
}

Having this output :

Choose from following options: 'Yes' or 'No' or 'Maybe' or 'Probably' or 'Never'
Sign up to request clarification or add additional context in comments.

Comments

2

According to Add object to ArrayList at specified index

List.add(1, "or") 
List.add(3, "or")

This should solve your problem.

3 Comments

I believe it would need to be 3 in the second add.
@CydrickT - you really should change that 4 to 3, cause this is not working.
Changed it. Thanks for the notification.

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.