当前位置: 首页 > article >正文

Java 字符串、数组、ArrayList之间的相互转换

1. 数组转字符串

复制代码

import java.util.Arrays;

public class Test02 {
    public static void main(String[] args){
        String[] scores1 = new String[]{"10","20","30","40","50"};
        String[] scores2 = {"10","20","30","40","50"};

        // 数组转字符串
        // 1.第一种方法
        String str = Arrays.toString(scores1);
        System.out.println(str); // [10, 20, 30, 40, 50]
        // 2.第二种方法
        String str2 = "";
        for (int i = 0; i < scores1.length; i++) {
            str2 += scores1[i] + ",";
        }
        str2 = str2.substring(0, str2.length() - 1);
        System.out.println(str2); // 10,20,30,40,50
        // 3.第三种方法(相比第二种,更推荐第三种)
        StringBuilder stringBuilder = new StringBuilder();
        for (String score : scores1) {
            stringBuilder.append(score).append(",");
        }
        // String str3 = new StringBuilder(stringBuilder.substring(0, stringBuilder.length() - 1)).toString();
        String str3 = stringBuilder.toString().substring(0, stringBuilder.length() - 1);
        System.out.println(str3); // 10,20,30,40,50
  }
}

复制代码

2. 数组转ArrayList

复制代码

import java.util.Arrays;

public class Test02 {
    public static void main(String[] args){
        // 数组转ArrayList
        String[] scores1 = new String[]{"10","20","30","40","50"};
        // 1.第一种方法
        ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(scores1));
        System.out.println(arrayList); // [10, 20, 30, 40, 50]

        List<String> list = Arrays.asList(scores1);
        System.out.println(list); // [10, 20, 30, 40, 50]
        // 2.第二种方法
        List<String> list2 = new ArrayList<String>();
        for(String score : scores1){
            list2.add(score);
        }
  }
}

复制代码

3. ArrayList转数组

复制代码

import java.util.Arrays;

public class Test02 {
    public static void main(String[] args){
        // ArrayList转数组
        List<String> list = new ArrayList(){{add("10");add("20");add("30");add("40");add("50");}};
        System.out.println(list); // [10, 20, 30, 40, 50]

        //使用toArray(T[] a)方法
        String[] arrayList = list.toArray(new String[0]);
        System.out.println(Arrays.toString(arrayList)); // [10, 20, 30, 40, 50]
  }
}

复制代码

 4. ArrayList转字符串

复制代码

public class Test02 {
    public static void main(String[] args){
        // ArrayList转字符串
        List<String> list = new ArrayList(){{add("10");add("20");add("30");add("40");add("50");}};
        System.out.println(list); // [10, 20, 30, 40, 50]

        String str = list.toString();
        System.out.println(str); // [10, 20, 30, 40, 50]
  }
}

复制代码

5. 字符串转数组

复制代码

import java.util.Arrays;

public class Test02 {
    public static void main(String[] args){
        // 字符串转数组
        String str = "10,20,30,40,50";
        String[] strArr = str.split(",");
        System.out.println(Arrays.toString(strArr)); // [10, 20, 30, 40, 50]
  }
}

复制代码


http://www.kler.cn/a/296724.html

相关文章:

  • 秒懂:进程相关的操作
  • MyBatis-MappedStatement什么时候生成?QueryWrapper如何做到动态生成了SQL?
  • Java 8 时间日期处理
  • WebRTC 代码实现详述
  • SpringCloud-03 LoadBalancer服务调用与负载均衡
  • 《深入解析 ESLint 配置:从零到精通》
  • 如何在Mac电脑上本地部署Stable Diffusion:详细教程(webUI)
  • mac上jmeter的安装和使用
  • 面试准备-7
  • PAT甲级-1029 Median
  • AndroidStudio清除重置Http Proxy代理的方式
  • 论文翻译:ICLR-2024 PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS
  • HRGraph: 利用大型语言模型(LLMs)构建基于信息传播的HR数据知识图谱与职位推荐
  • 3.创建型设计模式详解:生成器模式与原型模式的深度解析
  • 如何在VSCODE中查看西门子PLC的SCL程序?
  • 达梦数据库:dm与mysql语法差异(select)
  • CAP (C# Distributed Application Framework)
  • [Linux Kernel Block Layer第一篇] block layer架构设计
  • Spring Boot项目中如何解决循环依赖
  • 大模型构建合作性的Agent,多代理框架MetaGpt