String srt = "236549"; Arrays.asList(srt .split(","))
时间: 2023-12-05 17:05:00 浏览: 102
This code will not compile because the split function is being called on a string without any delimiter specified. The split function splits a string into an array of strings based on a specified delimiter. In this case, there is no comma in the string "236549", so splitting it by comma will result in an array with only one element.
To split the string into an array with each digit as an element, the code should be:
String srt = "236549";
List<String> list = Arrays.asList(srt.split(""));
System.out.println(list);
Output: [2, 3, 6, 5, 4, 9]
相关问题
优化以下代码 List<String> rualist = new ArrayList<>(); String createUserAccount = spaceCard.getCreateUserAccount(); String testUserAccount = spaceCard.getTestUserAccount(); String principalPerson = spaceCard. getPrincipalPerson(); String productUserAccount = spaceCard.getProductUserAccount(); String concernPerson = spaceCard . getConcernPerson(); if(createUserAccount!=null){ rualist.addAll(Arrays.asList(createUserAccount.split( ","))); } if(testUserAccount!=null){ rualist.addAll(Arrays.asList(testUserAccount.split( ","))); } if(principalPerson!=null){ rualist.addAll(Arrays.asList(principalPerson.split (","))); } if (productUserAccount!=null){ rualist.addAll(Arrays.asList(productUserAccount.split(","))); } if(concernPerson!=null){ rualist.addAll(Arrays.asList(concernPerson.split(","))); } String value = StringUtils.join(rualist.toArray(),",");
可以考虑使用 Java 8 的 Stream API 简化代码,如下所示:
```
List<String> rualist = Stream.of(spaceCard.getCreateUserAccount(), spaceCard.getTestUserAccount(),
spaceCard.getPrincipalPerson(), spaceCard.getProductUserAccount(), spaceCard.getConcernPerson())
.filter(Objects::nonNull)
.flatMap(s -> Arrays.stream(s.split(",")))
.collect(Collectors.toList());
String value = String.join(",", rualist);
```
这样可以将代码压缩为几行,并且使用了函数式编程的思想,提高了代码的可读性和简洁性。
List<String> list = Arrays.asList(arr);Arrays报错 List<String> list = Arrays.asList(arr); List<String> list = Arrays.asList(arr);
Arrays.asList()方法返回的是一个固定大小的List,不能进行添加或删除操作,只能进行修改操作。如果你想要一个可变的List,可以使用java.util.ArrayList来代替Arrays.asList()方法返回的List。如果你使用Arrays.asList()方法将int[]转成ArrayList,会出现类型不匹配的错误,因为Arrays.asList()方法只能将数组转换为List<Object>类型,而int[]是一个基本数据类型,不是一个对象。如果你想要将int[]转换为List<Integer>类型的ArrayList,可以使用Java 8中的IntStream来实现,例如:
```java
int[] arr = {1, 2, 3};
List<Integer> list = IntStream.of(arr).boxed().collect(Collectors.toList());
```
阅读全文
相关推荐

















