如何用Java创建新列表

我们创建Set为:


Set myset = new HashSet()

我们如何List用Java 创建一个?


米琪卡哇伊
浏览 814回答 3
3回答

慕哥9229398

List myList = new ArrayList();或使用泛型(Java 7或更高版本)List<MyType> myList = new ArrayList<>();或带有泛型(旧的Java版本)List<MyType> myList = new ArrayList<MyType>();

元芳怎么了

此外,如果您想创建一个包含所有内容的列表(尽管它将是固定大小的):List<String> messages = Arrays.asList("Hello", "World!", "How", "Are", "You");

守候你守候我

让我总结一下并添加一些内容:JDK1. new ArrayList<String>();2. Arrays.asList("A", "B", "C")番石榴1. Lists.newArrayList("Mike", "John", "Lesly");2. Lists.asList("A","B", new String [] {"C", "D"});不可变列表1. Collections.unmodifiableList(new ArrayList<String>(Arrays.asList("A","B")));2. ImmutableList.builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Guava&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add("A")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .add("B").build();3. ImmutableList.of("A", "B");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Guava4. ImmutableList.copyOf(Lists.newArrayList("A", "B", "C"));&nbsp; &nbsp; &nbsp;// Guava空的不可变列表1. Collections.emptyList();2. Collections.EMPTY_LIST;字符列表1. Lists.charactersOf("String")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Guava2. Lists.newArrayList(Splitter.fixedLength(1).split("String"))&nbsp; // Guava整数列表Ints.asList(1,2,3);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Guava
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java