我希望创建一个能够随时间改变大小的数组,因为数组的大小是不可预测的,并且我不想创建一个巨大的随机数,这会浪费内存,所以每次按下按钮时我都需要数组加一。
private String[][] lyricLineInfo = new String[x][5];
x 的位置是按下按钮时数组必须增长的位置,5 是一个常量。所以我需要 x 按钮加一而不溢出。我可以用这样的东西来做吗?
lyricLineInfo[lyricLineInfo.length + 1][4] = fieldLyrics.getText();
无论如何,提前致谢!
I'm looking to create an array that will be able to change size over time because the size of the array is unpredictable and I don't want to create a huge random number that will waste memory so every time a button is pressed I need the array to grow by one.
private String[][] lyricLineInfo = new String[x][5];
In the place of x is where the array must grow upon the button push and 5 is a constant. So I need the x button to grow by one without overflowing. Can I do it by using something like this?
lyricLineInfo[lyricLineInfo.length + 1][4] = fieldLyrics.getText();
Anyways thanks in advance!
发布评论
评论(7)
使用
ArrayList
(请参阅文档 此处)。它会自动生长。 (它使用一个内部数组,当它需要增长时,它实际上不会仅增长 1。由于增长是一项昂贵的操作,因此它会增长一些更大的量,以便在必须再次增长之前它可以吸收更多的项目。)< strong>编辑
例如,您可以按照以下方式对原始帖子的两行进行重新编码:
第二行假设
fieldLyrics.getText()
返回一个String[].如果我误解了你的意图并且它返回一个字符串,那么你可以执行以下操作:
如果第二个索引并不总是 5 长,你也可以有一个 ArrayList 的 ArrayList:
那么你可以
lyricLineInfo.add(new ArrayList< ;String>());
扩展数组。编辑2
@clankfan1 - 在您的评论中,您询问如何执行特定操作。假设我们使用
ArrayList>
结构。它会是这样的:显然,这个逻辑适合放入一个单独的方法中。
编辑 3
如果您需要
lyricLineInfo
的元素为String[]
类型,则每个元素都是不同的数组至关重要。以下是添加元素的几种编码样式:Use an
ArrayList<String[]>
(see the docs here). It will grow automatically. (It uses an internal array that doesn't actually grow by just 1 when it needs to grow. Since growing is an expensive operation, it grows by some larger amount so it can absorb a few more items before having to grow again.)EDIT
For example, here's how you could recode the two lines of your original post:
The second line assumes that
fieldLyrics.getText()
returns aString[]
. If I misunderstood your intent and it returns a String, then you could do the following:If the second index isn't always 5 long, you can also have an ArrayList of ArrayLists:
Then you could
lyricLineInfo.add(new ArrayList<String>());
to extend the array.EDIT 2
@clankfan1 - In your comment, you asked how to do a particular operation. Let's say we're using the
ArrayList<ArrayList<String>>
structure. It would go something like this:Obviously, this logic is amenable to being put into a separate method.
EDIT 3
If you need the elements of
lyricLineInfo
to be of typeString[]
, it is vital that each element be a distinct array. Here are a few coding styles for adding elements:您可以使用
java.util.Vector
。You could use
java.util.Vector<String[]>
.像这样使用
ArrayList
:ArrayList
是java中的灵活数组。当你想添加一些东西时,请执行以下操作:Use an
ArrayList
like this:ArrayList
s are flexible arrays in java. When you want to add something do this:使用列表代替:
然后添加到您使用的列表中:
并让您执行以下操作:
Use a List instead:
Then to add to the list you use:
and to get you do:
如果您需要一个未定义大小的数组,为什么不考虑使用集合呢? :)
Why don't you think about using Collections if you need an array with undefined size? :)
您不能使用数组来做到这一点,但可以使用 List 对象。你可以尝试这样的事情:
那么,假设 fieldLyrics.getText() 返回一个 String[],你会这样做:
You can't do that with an array, but you can use a List object. You could try something like this:
So then, assuming fieldLyrics.getText() returns a String[], you would do:
如果您只添加新元素并迭代列表中的所有元素,则应该使用
LinkedList
代替。Java 中大量使用集合。您应该检查 Java 集合框架 - 教程
If you only add new elements and iterate over all elements in list you should use
LinkedList
instead.Collections are heavily used in Java. You should check Java Collections Framework - tutorial