[英]create array in Java without setting number of objects?
I just started learning Java and found that you have to set number of objects in an array before hand. 我刚刚开始学习Java,发现您必须事先设置数组中的对象数。 so how can I make an array withou setting number of objects in an array like done in objective c. 所以我怎么能像在目标c中那样在不设置数组对象的情况下制作一个数组呢? I know I can do something like this 我知道我可以做这样的事情
int a[] = {1,3,1,3,41,34}
but in this case you have to put all the objects at once is there a way to do that? 但是在这种情况下,您必须一次放置所有对象,有没有办法做到这一点?
thanks 谢谢
Arrays in Java are not resizeable. Java中的数组不可调整大小。 If you need that kind of capability, you should look into the List<>
interface, and the classes that implements it (including ArrayList<>
, LinkedList<>
, etc.) 如果需要这种功能,则应查看List<>
接口以及实现该接口的类(包括ArrayList<>
, LinkedList<>
等)。
You don't have to put all of the objects in, but at the very least you'll need to know how many objects you are going to put in, like so: 您不必放入所有对象,但是至少您需要知道要放入多少个对象,如下所示:
int[] a = new int[6];
If you need it to be dynamic you'll have to use a List, or something like it. 如果您需要它是动态的,则必须使用一个List或类似的东西。
You'll have to use ArrayList<Integer>
if you don't know the number of elements before-hand. 如果您事先不知道元素数量,则必须使用ArrayList<Integer>
。
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(3);
...
Arrays have a fixed number of objects in them. 数组中有固定数量的对象。
You can allocate them empty, with: 您可以使用以下方法将它们分配为空:
Object[] objects = new Object[100]; // 100 null objects
For a variable-length list, try List. 对于可变长度列表,请尝试使用List。 like: 喜欢:
List<String> strings = new ArrayList<String>(); // arraylist is one of several kinds of list.
Like everyone else said, you will have to use lists.... Just wanted to add a little comparison to Objective-C(well Apples framework classes for objective-C but anyway) 就像其他所有人所说的,您将不得不使用列表。...只是想对Objective-C进行一些比较(苹果公司的Objective-C框架类还是无论如何)
Unlike in Obective-C where you can init NS(Mutable)Arrays and whatnot with a nil terminated list, in Java you cannot add default values to your lists when you create them, you have to repeatedly call the add method to add the objects. 与Obective-C中可以初始化NS(Mutable)Arrays以及不使用终止列表为nil的东西不同,在Java中,创建默认值时不能在列表中添加默认值,而必须重复调用add方法来添加对象。 Java introduced a variable number of arguments in Java 1.5, but as of 6.0 has not added constructors that use this feature to any of their implementations of the List interface. Java在Java 1.5中引入了可变数量的参数,但是从6.0版本开始,尚未将使用此功能的构造函数添加到List接口的任何实现中。
Check out the google guava library, specifically http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Lists.html . 查看Google番石榴库,尤其是http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Lists.html 。 The lists class lets you create a mutable arraylist with initial elements in a one-liner. 使用lists类,您可以在一个直线中创建具有初始元素的可变数组列表。
Tangentially, the Guava libraries are very useful for common day-to-day collection type stuff. 相切地讲,Guava库对于常见的日常收集类型的东西非常有用。 Check them out. 去看一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.