Java中的固定大小列表 [英] fixed size list in Java

查看:577
本文介绍了Java中的固定大小列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以定义一个固定大小为100的列表?如果不是为什么在Java中不可用?

解决方案


$ b

Commons库提供了一个内置的FixedSizeList不支持添加删除清除方法(但是set方法是允许的,因为它不会修改 List 的大小)。换句话说,如果您尝试调用这些方法之一,您的列表仍保持相同的大小。



要创建固定大小的列表,只需调用

 列表< YourType> fixed = FixedSizeList.decorate(Arrays.asList(new YourType [100])); 


is it possible to define a list with a fixed size that's 100? If not why isn't available in Java?

解决方案

Yes,

Commons library provides a built-in FixedSizeList which does not support the add, remove and clear methods (but the set method is allowed because it does not modify the List's size). In other words, if you try to call one of these methods, your list still retain the same size.

To create your fixed size list, just call

List<YourType> fixed = FixedSizeList.decorate(Arrays.asList(new YourType[100]));

这篇关于Java中的固定大小列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆