如何在java中初始化动态数组? [英] How to initialize a dynamic array in java?
问题描述
如果我有一个类需要返回一个变量维的字符串数组(并且该维度只能在运行该类的某个方法时确定),那么如何在类的构造函数中声明动态数组呢? / p>
如果问题不够明确,
在php中我们可以简单地将字符串数组声明为 $ my_string_array = array();
并通过 $ my_string_array [] =新值添加元素;
上面的代码是什么,然后在java中?
您将需要查看 java.util包
,特别是 ArrayList
类。它有一些方法,如 .add()。remove()。indexof()。contains()。toArray()
等等。
If I have a class that needs to return an array of strings of variable dimension (and that dimension could only be determined upon running some method of the class), how do I declare the dynamic array in my class' constructor?
If the question wasn't clear enough,
in php we could simply declare an array of strings as $my_string_array = array();
and add elements to it by $my_string_array[] = "New value";
What is the above code equivalent then in java?
You will want to look into the java.util package
, specifically the ArrayList
class. It has methods such as .add() .remove() .indexof() .contains() .toArray()
, and more.
这篇关于如何在java中初始化动态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!