V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
singularity9866
V2EX  ›  Java

和各位 V 友们一起讨论一下 Java 基础,有谁知道输出多少?为什么吗?

  •  1
     
  •   singularity9866 · 145 天前 · 2186 次点击
    这是一个创建于 145 天前的主题,其中的信息可能已经有所发展或是发生改变。

    /**

    • @Author : @Singularity

    • @create @2023/9/27 17:29 */ public class Test {

      public static void main(String[] args) { List<String> list = List.of("1", "2", "3"); list.add("4"); System.out.println(list); //输出多少? List<String> collect = Stream.of("1", "2", "3").collect(Collectors.toList()); list.add("4"); System.out.println(collect); //输出多少? List<String> stringList = Arrays.asList("1", "2", "3"); stringList.add("4"); System.out.println(stringList); //输出多少? } }

    11 条回复    2023-12-06 10:58:36 +08:00
    Enzoliu
        1
    Enzoliu  
       145 天前   ❤️ 3
    这代码看的头疼,就不能格式化一下发出来吗?
    qczone
        2
    qczone  
       145 天前
    1. 异常,因为 List.of 创建的是一个不可变的列表,不能删除增加和修改元素。
    2. 应该是 collect.add("4")吧? 输出:[1, 2, 3, 4]
    3. 异常,Arrays.asList 创建的是一个可变列表,大小是固定的,可以更改列表中的元素,但不能添加或删除。
    YoungAD
        3
    YoungAD  
       145 天前
    @qczone Arrays.asList 不能 add 但是可以 set 好像
    qczone
        4
    qczone  
       145 天前
    @YoungAD 是的老哥
    wjx22
        5
    wjx22  
       145 天前
    ```
    public static void main(String[] args) {
    List<String> list = List.of("1", "2", "3");
    // list.add("4"); 报错
    System.out.println(list); //输出多少?
    List<String> collect = Stream.of("1", "2", "3").collect(Collectors.toList());
    collect.add("4");// 正常
    System.out.println(collect); //输出多少?
    List<String> stringList = Arrays.asList("1", "2", "3");
    // stringList.add("4"); 报错
    System.out.println(stringList); //输出多少?
    }
    ```
    yazinnnn0
        6
    yazinnnn0  
       145 天前
    有点过于八股了
    Leviathann
        7
    Leviathann  
       145 天前
    add 是陋习
    java 在朝着 fp 语言演进,比如 record 、List.of 统统都是 immutable ,pattern matching 也只适用于 immutable 的东西
    991547436
        8
    991547436  
       145 天前
    // 使用 List.of 创建不可变列表
    List<String> list = List.of("1", "2", "3");

    // 尝试添加元素会导致 UnsupportedOperationException
    // list.add("4");

    System.out.println(list);

    // 使用 ArrayList 创建可变列表
    List<String> mutableList = new ArrayList<>(Arrays.asList("1", "2", "3"));
    mutableList.add("4");
    System.out.println(mutableList);

    // 使用 Stream 创建不可变列表,然后转为可变列表
    List<String> collect = Stream.of("1", "2", "3").collect(Collectors.toList());
    collect.add("4"); // 这是可行的,因为是可变列表
    System.out.println(collect);

    // 使用 Arrays.asList 创建的列表是固定大小的,不支持添加或删除元素
    List<String> stringList = new ArrayList<>(Arrays.asList("1", "2", "3"));
    stringList.add("4");
    System.out.println(stringList);
    yajuusenpai
        9
    yajuusenpai  
       145 天前
    不可变列表是干啥的?
    oneisall8955
        10
    oneisall8955  
       145 天前 via Android
    看看源码,最后返回的 List 接口实现类是哪个就行了,很基础的东西
    yolee599
        11
    yolee599  
       144 天前
    @yajuusenpai #9 就是用来 return 一个只读列表用来遍历的,不允许通过这个列表 add
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1148 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:21 · PVG 02:21 · LAX 11:21 · JFK 14:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.