实现

点击查看代码
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;
    }
}

 posted on 2022-02-20 15:14  魂蛋  阅读(112)  评论(0编辑  收藏  举报