如何用空对象填充字符串数组?

发布于 2025-01-25 03:21:34 字数 1466 浏览 2 评论 0原文

因此,我创建了将容纳CD集合的数组。唯一的问题是最大数组大小是50,如果有50个对象,我如何用空对象填充数组?:

public static void main(String[] args) throws IOException 
  {
    final int MAX_ARRAY_SIZE = 50;
    final String FILENAME = "Collection.txt";

    CDOutput out = new CDOutput();
    CDInput in = new CDInput();

    int count = 0;  // Counter to keep track of number of elements in the array
    int choice = 0; // Menu choice

    String[] songArray = new String[MAX_ARRAY_SIZE]; // Create array to hold song collection

    {
    songArray[0] = new String("");// Fill array with empty objects ????????
    songArray[1] = new String("");
    songArray[2] = new String("");
    songArray[3] = new String("");
    songArray[4] = new String("");
    songArray[5] = new String("");
    songArray[6] = new String("");
    songArray[6] = new String("");
    songArray[7] = new String("");
    songArray[8] = new String("");
    songArray[9] = new String("");
    songArray[10] = new String("");
    songArray[11] = new String("");
    songArray[12] = new String("");
    songArray[13] = new String("");
    songArray[14] = new String("");
    songArray[15] = new String("");
    songArray[16] = new String("");
    songArray[17] = new String("");
    songArray[18] = new String(""); //and so on......
    }

我知道这是不正确的...

编辑(事后看来确实很容易):

String[] songArray = new String[50];
for (int i = 0; i < 50; i++)
    songArray[i] = new String("");

So I create the array that will hold a cd collection. The only problem is the maximum array size is 50, how do I fill the array with empty objects if there's 50 objects?:

public static void main(String[] args) throws IOException 
  {
    final int MAX_ARRAY_SIZE = 50;
    final String FILENAME = "Collection.txt";

    CDOutput out = new CDOutput();
    CDInput in = new CDInput();

    int count = 0;  // Counter to keep track of number of elements in the array
    int choice = 0; // Menu choice

    String[] songArray = new String[MAX_ARRAY_SIZE]; // Create array to hold song collection

    {
    songArray[0] = new String("");// Fill array with empty objects ????????
    songArray[1] = new String("");
    songArray[2] = new String("");
    songArray[3] = new String("");
    songArray[4] = new String("");
    songArray[5] = new String("");
    songArray[6] = new String("");
    songArray[6] = new String("");
    songArray[7] = new String("");
    songArray[8] = new String("");
    songArray[9] = new String("");
    songArray[10] = new String("");
    songArray[11] = new String("");
    songArray[12] = new String("");
    songArray[13] = new String("");
    songArray[14] = new String("");
    songArray[15] = new String("");
    songArray[16] = new String("");
    songArray[17] = new String("");
    songArray[18] = new String(""); //and so on......
    }

I know this cannot be correct...

EDIT (this truly was quite easy in hindsight):

String[] songArray = new String[50];
for (int i = 0; i < 50; i++)
    songArray[i] = new String("");

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

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

发布评论

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

评论(3

江湖彼岸 2025-02-01 03:21:34

arrays.fill()也许可以为您提供帮助: http:// /docs.oracle.com/javase/7/docs/api/java/java/util/arrays.html

另外,如果您使用的是apache commons lang,则可以使用 arrayutils。 nulltoEmpty()

Arrays.fill() maybe can help you: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

Also, if you are using Apache Commons Lang, you can use ArrayUtils.nulltoEmpty()

美羊羊 2025-02-01 03:21:34

使用数组#填充。有关更多详细信息,请查看 javadoc 。一个典型的用途如下:

String[] a = new String[5];
String stringToFillWith = "";
Arrays.fill(a, 0, a.length - 1, stringToFillWith);

edit :两个数字0和a.length -1是用给定值填充的初始和最终索引(在这种情况下为,StringTofillwith)。

Use Arrays#fill. For more details, check out the Javadoc. A typical use is as follows:

String[] a = new String[5];
String stringToFillWith = "";
Arrays.fill(a, 0, a.length - 1, stringToFillWith);

Edit: The two numbers 0 and a.length - 1 are the initial and final indices to be filled with the given value (in this case, stringToFillWith).

甜`诱少女 2025-02-01 03:21:34

在Java中,每当您初始化数组时,数组都会具有一些“填充”所有新形成的数组的默认值。对于数值值,它们为零,null存储对象的数组:

  • boolean []false
  • char [] :'\ u0000'
  • byte []short []int [],,长[]0
  • double []float []。 :0.0
  • string []object []

。 /code>仅以null值开始,然后才添加“ CD”,然后不需要进一步的工作。 “”值开始

import java.util.Arrays;
....
String[] songArray = new String[MAX_ARRAY_SIZE];
Arrays.fill(songArray, "");

如果要songarray以所有 代码> 循环,而不是复制和粘贴一行代码50次:

for (int i = 0; i < MAX_ARRAY_SIZE; i++)
{
    songArray[i] = "";
}

In Java, whenever you initialize an array, the array will have some default value that "fills up" all of the newly formed array. For numerical values, they are zero, and null for arrays storing objects:

  • boolean[]: false
  • char[]: '\u0000'
  • byte[], short[], int[], , long[] : 0
  • double[], float[]. : 0.0
  • String[], Object[], etc. : null

If you want songArray to begin with only null values before they have "CDs" added, then no further work is needed. If you want songArray to begin with all "" values then use Arrays.fill:

import java.util.Arrays;
....
String[] songArray = new String[MAX_ARRAY_SIZE];
Arrays.fill(songArray, "");

Note that you can also easily do this manually using a for loop, rather than copying and pasting a line of code 50 times:

for (int i = 0; i < MAX_ARRAY_SIZE; i++)
{
    songArray[i] = "";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文