在Java中是否可以定义一个固定大小为100的列表?如果不能,为什么Java不支持这个功能?
如果我的记忆没错,这应该就可以了:
List<MyType> fixed = Arrays.asList(new MyType[100]);
add
方法,因为它已经有了100个空条目。请使用set
方法来设置值。请注意保持原意,使翻译更加通俗易懂,不提供额外的解释或信息。 - McDowellMyType
是参数化类型,则此方法无法起作用。例如,List<List<String>> fixed = Arrays.asList(new List<String>[100]);
会生成编译错误(在早期的Java版本中,会生成警告)。请参阅Java教程中的泛型限制。 - Ted HoppArrays.asList()
总是创建一个不可变的列表。 - DaithiGArrays.asList(array)
创建列表包装器。包装器将允许您执行get
和set
等操作,但add
和remove
操作将引发异常。FixedSizeList
类。但请注意,此包装器无法阻止其他内容更改原始列表的大小,如果发生这种情况,则封装的列表将反映这些更改。
FixedSizeList
是的,
Apache Commons库提供了FixedSizeList
类,该类不支持add
、remove
和clear
方法(但允许使用set方法,因为它不会修改List
的大小)。Eclipse Collections中的FixedSizeList
也是如此。如果您尝试调用其中一个方法,则列表的大小仍然相同。
要创建固定大小的列表,只需调用:
List<YourType> fixed = FixedSizeList.decorate(Arrays.asList(new YourType[100]));
如果您想要查看指定列表的不可修改视图,或只读访问内部列表,可以使用 unmodifiableList
。
List<YourType> unmodifiable = java.util.Collections.unmodifiableList(internalList);
FixedSizeList.decorate(...)
是用于包装那些尚未固定大小的列表的。在这里使用它是多余的。 - Stephen C可以。您可以将Java数组传递给 Arrays.asList(Object[])。
List<String> fixedSizeList = Arrays.asList(new String[100]);
您无法向FixedSizeList中插入新的字符串(它已经有100个元素)。您只能像这样设置其值:
fixedSizeList.set(7, "new value");
这样你就有了一个固定大小的列表。这个东西就像一个数组一样运行,我想不出使用它的好理由。我很想知道为什么你希望你的固定大小的集合是一个列表而不是只使用一个数组。
import java.util.ArrayList;
public class FixedSizeList<T> extends ArrayList<T> {
public FixedSizeList(int capacity) {
super(capacity);
for (int i = 0; i < capacity; i++) {
super.add(null);
}
}
public FixedSizeList(T[] initialElements) {
super(initialElements.length);
for (T loopElement : initialElements) {
super.add(loopElement);
}
}
@Override
public void clear() {
throw new UnsupportedOperationException("Elements may not be cleared from a fixed size List.");
}
@Override
public boolean add(T o) {
throw new UnsupportedOperationException("Elements may not be added to a fixed size List, use set() instead.");
}
@Override
public void add(int index, T element) {
throw new UnsupportedOperationException("Elements may not be added to a fixed size List, use set() instead.");
}
@Override
public T remove(int index) {
throw new UnsupportedOperationException("Elements may not be removed from a fixed size List.");
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException("Elements may not be removed from a fixed size List.");
}
@Override
protected void removeRange(int fromIndex, int toIndex) {
throw new UnsupportedOperationException("Elements may not be removed from a fixed size List.");
}
}
public class LimitedArrayList<T> extends ArrayList<T>{
private int limit;
public LimitedArrayList(int limit){
this.limit = limit;
}
@Override
public void add(T item){
if (this.size() > limit)
throw new ListTooLargeException();
super.add(item);
}
// ... similarly for other methods that may add new elements ...
@SuppressWarnings("unchecked")
public static <T> List<T> newFixedSizeList(int size) {
return (List<T>)Arrays.asList(new Object[size]);
}
List<String> s = newFixedSizeList(3); // All elements are initialized to null
s.set(0, "zero");
s.add("three"); // throws java.lang.UnsupportedOperationException
static class FixedSizeCircularReference<T> {
T[] entries
FixedSizeCircularReference(int size) {
this.entries = new Object[size] as T[]
this.size = size
}
int cur = 0
int size
void add(T entry) {
entries[cur++] = entry
if (cur >= size) {
cur = 0
}
}
List<T> asList() {
List<T> list = new ArrayList<>()
int oldest = (cur == size - 1) ? 0 : cur
for (int i = 0; i < this.entries.length; i++) {
def e = this.entries[oldest + i < size ? oldest + i : oldest + i - size]
if (e) list.add(e)
}
return list
}
}
FixedSizeCircularReference<String> latestEntries = new FixedSizeCircularReference(100)
latestEntries.add('message 1')
// .....
latestEntries.add('message 1000')
latestEntries.asList() //Returns list of '100' messages
List<Integer> list = Arrays.stream(new int[100])
.boxed()
.collect(Collectors.toList());
或者老式的方法,这将导致由指定数组支持的固定大小列表:
List<Integer> list = Arrays.asList(new Integer[100]);
add()
,应该发生什么?抛出异常、无操作还是舍弃现有元素以保留新元素。 - BozhoList
接口的规范。 ;) - Bozho