简体   繁体   中英

How to add strings into an arraylist between two strings

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"?

According to Add object to ArrayList at specified index

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

This should solve your problem.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM