通过将输入的5个字符串,存储在arraylist,转换成字符串输出

实现

点击查看代码
package ArrayListTest;

import java.util.ArrayList;
import java.util.Scanner;

public class bianli {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        ArrayList<String> arrayList= new ArrayList<String>();
        for (int i = 0; i < 5; i++) {
            arrayList.add(i,scanner.next());
        }
        String string = arraytoString(arrayList);
        System.out.println(string);
    }

    //输出集合中的元素
    public static String arraytoString(ArrayList<String> array) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("[");
        for (int i = 0; i < array.size(); i++) {
            if (i == array.size() - 1) {
                stringBuilder.append(array.get(i));
            } else {
                stringBuilder.append(array.get(i)).append(",");
            }
        }
        stringBuilder.append("]");
        String string = stringBuilder.toString();
        return string;
    }
}

上一篇:面试总结-进程、线程与多线程


下一篇:python 列表append和 的区别??