McD*_*ell 35
如果内存服务,这应该这样做:
List<MyType> fixed = Arrays.asList(new MyType[100]);
Ste*_*n C 27
你的问题是错误的,或者你的思维模型不正确List.
Java列表是对象的集合......列表的元素.列表的大小是该列表中元素的数量.如果您希望修复该大小,则意味着您无法添加或删除元素,因为添加或删除元素会违反"固定大小"约束.
实现"固定大小"列表的最简单方法(如果这真的是你想要的!)是将元素放入数组中,然后Arrays.asList(array)创建列表包装器.包装器将允许你这样做操作,如get和set,但add和remove操作将抛出异常.
如果要为现有列表创建固定大小的包装器,则可以使用Apache commons FixedSizeList类.但请注意,此包装器无法阻止其他更改原始列表大小的内容,如果发生这种情况,则包装列表可能会反映这些更改.(IMO,javadoc for FixedSizeList是可悲的.它不会尝试记录更改包装列表时类的行为.您需要阅读源代码...并希望它们不会改变您的行为不重视.)
另一方面,如果你真的想要一个对其大小有固定限制(或限制)的列表类型,那么你需要创建自己的List类来实现它.例如,您可以创建一个包装类,在各种add/ addAll和remove/ removeAll/ retainAll操作中实现相关检查.(remove如果支持它们,则在迭代器方法中.)
那么为什么Java Collections框架没有实现这些呢?这就是为什么我这么认为:
Collections.sort.Art*_*ald 18
是的,
共享库提供了一个内置的FixedSizeList,其不支持add,remove和clear方法(但是被允许的组的方法,因为它不修改List的大小).换句话说,如果您尝试调用其中一种方法,则列表仍保持相同的大小.
要创建固定大小列表,只需致电
List<YourType> fixed = FixedSizeList.decorate(Arrays.asList(new YourType[100]));
unmodifiableList如果需要指定列表的不可修改视图或对内部列表的只读访问权限,则可以使用.
List<YourType> unmodifiable = java.util.Collections.unmodifiableList(internalList);
sna*_*ile 13
是.您可以将java数组传递给Arrays.asList(Object []).
List<String> fixedSizeList = Arrays.asList(new String[100]);
您不能将新的字符串插入fixedSizeList(它已经有100个元素).您只能像这样设置其值:
fixedSizeList.set(7, "new value");
这样你就有了一个固定大小的列表.这个东西就像一个数组,我想不出一个使用它的好理由.我很想知道为什么你希望固定大小的集合成为一个列表,而不是只使用一个数组.
通常,固定大小列表的替代方案是Java阵列.默认情况下,列表允许在Java中增长/缩小.但是,这并不意味着您不能拥有固定大小的列表.您需要做一些工作并创建自定义实现.
您可以使用clear,add和remove方法的自定义实现扩展ArrayList.
例如
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.");
    }
}
| 归档时间: | 
 | 
| 查看次数: | 112157 次 | 
| 最近记录: |