Java - 创建未定义的数组

发布于 2024-12-20 04:33:35 字数 343 浏览 0 评论 0 原文

我希望创建一个能够随时间改变大小的数组,因为数组的大小是不可预测的,并且我不想创建一个巨大的随机数,这会浪费内存,所以每次按下按钮时我都需要数组加一。

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

因为看清所以看轻 2024-12-27 04:33:35

使用 ArrayList (请参阅文档 此处)。它会自动生长。 (它使用一个内部数组,当它需要增长时,它实际上不会仅增长 1。由于增长是一项昂贵的操作,因此它会增长一些更大的量,以便在必须再次增长之前它可以吸收更多的项目。)

< strong>编辑

例如,您可以按照以下方式对原始帖子的两行进行重新编码:

private ArrayList<String[]> lyricLineInfo = new ArrayList<String[]>();

lyricLineInfo.add(fieldLyrics.getText());

第二行假设 fieldLyrics.getText() 返回一个 String[].如果我误解了你的意图并且它返回一个字符串,那么你可以执行以下操作:

String[] nextStrings = new String[5];
nextStrings[4] = fieldLyrics.getText();
lyricLineInfo.add(nextStrings);

如果第二个索引并不总是 5 长,你也可以有一个 ArrayList 的 ArrayList:

private ArrayList<ArrayList<String>> lyricLineInfo
    = new ArrayList<ArrayList<String>>();

那么你可以 lyricLineInfo.add(new ArrayList< ;String>()); 扩展数组。

编辑2

@clankfan1 - 在您的评论中,您询问如何执行特定操作。假设我们使用 ArrayList> 结构。它会是这样的:

ArrayList<ArrayList<String>> lyricLineInfo = new ArrayList<ArrayList<String>>();

ArrayList<String> line = new ArrayList<String>();
line.add("true");
line.add("true");
line.add("0.0");
line.add("5.0");
line.add("First Line");
lyricLineInfo.add(line);
line = new ArrayList<String>(); // don't use clear(): need a new object here
line.add("false");
line.add("false");
line.add("5.0");
line.add("10.0");
line.add("Second Line");
lyricLineInfo.add(line);

String secondLineTitle = lyricLineInfo.get(1).get(4); // will be "Second Line"

显然,这个逻辑适合放入一个单独的方法中。

编辑 3

如果您需要 lyricLineInfo 的元素为 String[] 类型,则每个元素都是不同的数组至关重要。以下是添加元素的几种编码样式:

ArrayList<String[]> lyricLineInfo = new ArrayList<String[]>();

String[] line = { "true", "true", "0.0", "5.0", "First Line" };
lyricLineInfo.add(line);
// now for a second style:
line = new String[5];
line[0] = "false";
line[1] = "false";
line[2] = "5.0";
line[3] = "10.0";
line[4] = "Second Line";
lyricLineInfo.add(line);
// and a third style:
lyricLineInfo.add(new String[] {
    "false", "true", "10.0", "15.0", "Third Line"
});

String secondLineTitle = lyricLineInfo.get(1)[4]; // will be "Second Line"

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:

private ArrayList<String[]> lyricLineInfo = new ArrayList<String[]>();

lyricLineInfo.add(fieldLyrics.getText());

The second line assumes that fieldLyrics.getText() returns a String[]. If I misunderstood your intent and it returns a String, then you could do the following:

String[] nextStrings = new String[5];
nextStrings[4] = fieldLyrics.getText();
lyricLineInfo.add(nextStrings);

If the second index isn't always 5 long, you can also have an ArrayList of ArrayLists:

private ArrayList<ArrayList<String>> lyricLineInfo
    = new ArrayList<ArrayList<String>>();

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:

ArrayList<ArrayList<String>> lyricLineInfo = new ArrayList<ArrayList<String>>();

ArrayList<String> line = new ArrayList<String>();
line.add("true");
line.add("true");
line.add("0.0");
line.add("5.0");
line.add("First Line");
lyricLineInfo.add(line);
line = new ArrayList<String>(); // don't use clear(): need a new object here
line.add("false");
line.add("false");
line.add("5.0");
line.add("10.0");
line.add("Second Line");
lyricLineInfo.add(line);

String secondLineTitle = lyricLineInfo.get(1).get(4); // will be "Second Line"

Obviously, this logic is amenable to being put into a separate method.

EDIT 3

If you need the elements of lyricLineInfo to be of type String[], it is vital that each element be a distinct array. Here are a few coding styles for adding elements:

ArrayList<String[]> lyricLineInfo = new ArrayList<String[]>();

String[] line = { "true", "true", "0.0", "5.0", "First Line" };
lyricLineInfo.add(line);
// now for a second style:
line = new String[5];
line[0] = "false";
line[1] = "false";
line[2] = "5.0";
line[3] = "10.0";
line[4] = "Second Line";
lyricLineInfo.add(line);
// and a third style:
lyricLineInfo.add(new String[] {
    "false", "true", "10.0", "15.0", "Third Line"
});

String secondLineTitle = lyricLineInfo.get(1)[4]; // will be "Second Line"
几味少女 2024-12-27 04:33:35

您可以使用java.util.Vector

You could use java.util.Vector<String[]>.

拥有 2024-12-27 04:33:35

像这样使用ArrayList

private ArrayList<ArrayList<String>> lyricLineInfo = new ArrayList<ArrayList<String>>();

ArrayList是java中的灵活数组。当你想添加一些东西时,请执行以下操作:

lyricLineInfo.add(stringToBeAdded, index) //for the first dimension and
lyricLineInfo.get(firstIndex).add(stringToBeAdded, index); //for the second dimension

Use an ArrayList like this:

private ArrayList<ArrayList<String>> lyricLineInfo = new ArrayList<ArrayList<String>>();

ArrayLists are flexible arrays in java. When you want to add something do this:

lyricLineInfo.add(stringToBeAdded, index) //for the first dimension and
lyricLineInfo.get(firstIndex).add(stringToBeAdded, index); //for the second dimension
极致的悲 2024-12-27 04:33:35

使用列表代替:

private List<String[]> lyricLineInfo = new ArrayList<String[]>();

然后添加到您使用的列表中:

lyricLineInfo.add(new String[5]);

并让您执行以下操作:

// Get the 3rd element (array index 2).
String[] strings = lyricLineInfo.get(2);

Use a List instead:

private List<String[]> lyricLineInfo = new ArrayList<String[]>();

Then to add to the list you use:

lyricLineInfo.add(new String[5]);

and to get you do:

// Get the 3rd element (array index 2).
String[] strings = lyricLineInfo.get(2);
坐在坟头思考人生 2024-12-27 04:33:35

如果您需要一个未定义大小的数组,为什么不考虑使用集合呢? :)

Why don't you think about using Collections if you need an array with undefined size? :)

情绪失控 2024-12-27 04:33:35

您不能使用数组来做到这一点,但可以使用 List 对象。你可以尝试这样的事情:

private List<String[]> lyricLineInfo = new ArrayList<String[]>();

那么,假设 fieldLyrics.getText() 返回一个 String[],你会这样做:

lyricLineInfo.add(fieldLyrics.getText());

You can't do that with an array, but you can use a List object. You could try something like this:

private List<String[]> lyricLineInfo = new ArrayList<String[]>();

So then, assuming fieldLyrics.getText() returns a String[], you would do:

lyricLineInfo.add(fieldLyrics.getText());
孤独岁月 2024-12-27 04:33:35

如果您只添加新元素并迭代列表中的所有元素,则应该使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文