String数组与List<String>相互转化

           
package util.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TestPhone {
    public static void main(String[] args) {
        String[] telephone = { "123456789" };
        // Arrays.asList(telephone)会返回一个定长类;把String[]转为List<String>
        List<String> teles = new ArrayList<String>(Arrays.asList(telephone));
        teles.add("1234567890");
        // 去重
        List<String> finDis = teles.stream().distinct().collect(Collectors.toList());
        // 把List<String>转为String[]
        String[] finphone = new String[finDis.size()];
        finDis.toArray(finphone);
        int lenth = finphone.length;
        System.out.println("--->>最后结果长度:     " + lenth);
        System.out.println("--->>最后结果长度:     " + finphone[0]);
        System.out.println("--->>最后结果长度:     " + finphone[1]);
        System.out.println("--->>最后结果长度:     " + finphone);
        String name = "中国_北京市_大兴区";
        String[] ss = name.substring(3).split("_");
        int lenth1 = ss.length;
        System.out.println("--->>最后结果长度:     " + lenth1);
        for (int i = 0; i < lenth; i++) {
            System.out.println("--->>最后结果长度:     " + ss[i]);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值