在java中向数组添加值

时间:2021-04-17 13:21:16
int[] tall = new int[28123];

for (int j = 1;j <= 28123; j++){
    int x = 0;
    tall[x] = j;
    x++;
}

What is wrong with this code? Shouldn't this code do the following:

这段代码有什么问题?这段代码不应该执行以下操作:

  1. Create an array named tall with a size of 28123.
  2. 创建一个名为tall的数组,大小为28123。
  3. Make index 0 = 1, index 1 = 2 and so on.
  4. 使索引0 = 1,索引1 = 2,依此类推。

9 个解决方案

#1


5  

No, you're re-initializing x in every loop. Change to:

不,你在每个循环中重新初始化x。改成:

int[] tall = new int[28123];
int x = 0;

for (int j = 1;j<=28123;j++){
    tall[x] = j;
    x++;
}

Or, even better (since x is always equal to j-1):

或者,甚至更好(因为x总是等于j-1):

int[] tall = new int[28123];

for (int j = 1;j<=28123;j++){
    tall[j-1] = j;
}

#2


2  

I suggest you step through the code in your debugger as debugging programs is what it is for.

我建议您逐步调试调试器中的代码,因为调试程序就是它的用途。

What I would expect you would see is that every time the code loops int x = 0; is set.

我希望你会看到每次代码循环int x = 0;已设定。

#3


2  

You have not one, but many mistakes. It should be:

你没有一个,但有很多错误。它应该是:

int[] tall = new int[28123];

for (int j=0;j<28123;j++){
    tall[j] = j+1;
}

Your code is putting a 0 in all the positions of the array.

您的代码在数组的所有位置都放置了0。

Morover, it'll throw an exception, because the last index of the array is 28123-1 (arrays in Java start in 0!).

Morover,它会抛出异常,因为数组的最后一个索引是28123-1(Java中的数组从0开始!)。

#4


0  

You always set x to 0 before changing array's value.

在更改数组值之前,始终将x设置为0。

You can use:

您可以使用:

int[] tall = new int[28123];

for (int j = 0;j<28123;j++){
    // Or whatever value you want to set.
    tall[j] = j + 1;
}

Or just remove the initialization of x (int x=0) before the for loop.

或者只是在for循环之前删除x(int x = 0)的初始化。

#5


0  

You're always inserting values at index 0. Other places in the array never get filled with values.

您始终在索引0处插入值。数组中的其他位置永远不会填充值。

You should probably have this:

你应该这样:

for (int j = 1; j <= 28123; j++) {
    tall[j-1] = j;
}

#6


0  

put x=0 outside the for loop that is the problem

把x = 0放在for循环之外就是问题

#7


0  

I understand what you are trying to do, but I think you will be able to do better with the below code. Also, it's easy.

我理解你要做的是什么,但我认为你可以用下面的代码做得更好。而且,这很容易。

    int[] Tall = new int[28123];
    for(int i = 0; i<Tall.length ; i++){
    Tall[i]= i+1;
    }
    System.out.print(Arrays.toString(Tall));

The "Arrays.toString(int[])" method returns a string representation of the contents of the specified int array. Also, to use it you will have to use :

“Arrays.toString(int [])”方法返回指定int数组内容的字符串表示形式。此外,要使用它,您将不得不使用:

    import java.util.Arrays

The output will be like this :

输出将是这样的:

    [1,2,3,4,5,6,7,8,9,10,..............,28122,28123]

I hope this helps.

我希望这有帮助。

#8


0  

  • First line : array created.
  • 第一行:创建数组。
  • Third line loop started from j = 1 to j = 28123
  • 第三行循环从j = 1开始到j = 28123
  • Fourth line you give the variable x value (0) each time the loop is accessed.
  • 第四行,每次访问循环时都给出变量x值(0)。
  • Fifth line you put the value of j in the index 0.
  • 第五行,你将j的值放在索引0中。
  • Sixth line you do increment to the value x by 1.(but it will be reset to 0 at line 4)
  • 第六行你将x增加到1.(但它将在第4行重置为0)

#9


0  

public class Array {
    static int a[] = new int[101];
    int counter = 0;
    public int add(int num) {
        if (num <= 100) {
            Array.a[this.counter] = num;
            System.out.println(a[this.counter]);
            this.counter++;

            return add(num + 1);
        }
        return 0;
    }

    public static void main(String[] args) {
        Array c = new Array();
        c.add(1);
    }
}

#1


5  

No, you're re-initializing x in every loop. Change to:

不,你在每个循环中重新初始化x。改成:

int[] tall = new int[28123];
int x = 0;

for (int j = 1;j<=28123;j++){
    tall[x] = j;
    x++;
}

Or, even better (since x is always equal to j-1):

或者,甚至更好(因为x总是等于j-1):

int[] tall = new int[28123];

for (int j = 1;j<=28123;j++){
    tall[j-1] = j;
}

#2


2  

I suggest you step through the code in your debugger as debugging programs is what it is for.

我建议您逐步调试调试器中的代码,因为调试程序就是它的用途。

What I would expect you would see is that every time the code loops int x = 0; is set.

我希望你会看到每次代码循环int x = 0;已设定。

#3


2  

You have not one, but many mistakes. It should be:

你没有一个,但有很多错误。它应该是:

int[] tall = new int[28123];

for (int j=0;j<28123;j++){
    tall[j] = j+1;
}

Your code is putting a 0 in all the positions of the array.

您的代码在数组的所有位置都放置了0。

Morover, it'll throw an exception, because the last index of the array is 28123-1 (arrays in Java start in 0!).

Morover,它会抛出异常,因为数组的最后一个索引是28123-1(Java中的数组从0开始!)。

#4


0  

You always set x to 0 before changing array's value.

在更改数组值之前,始终将x设置为0。

You can use:

您可以使用:

int[] tall = new int[28123];

for (int j = 0;j<28123;j++){
    // Or whatever value you want to set.
    tall[j] = j + 1;
}

Or just remove the initialization of x (int x=0) before the for loop.

或者只是在for循环之前删除x(int x = 0)的初始化。

#5


0  

You're always inserting values at index 0. Other places in the array never get filled with values.

您始终在索引0处插入值。数组中的其他位置永远不会填充值。

You should probably have this:

你应该这样:

for (int j = 1; j <= 28123; j++) {
    tall[j-1] = j;
}

#6


0  

put x=0 outside the for loop that is the problem

把x = 0放在for循环之外就是问题

#7


0  

I understand what you are trying to do, but I think you will be able to do better with the below code. Also, it's easy.

我理解你要做的是什么,但我认为你可以用下面的代码做得更好。而且,这很容易。

    int[] Tall = new int[28123];
    for(int i = 0; i<Tall.length ; i++){
    Tall[i]= i+1;
    }
    System.out.print(Arrays.toString(Tall));

The "Arrays.toString(int[])" method returns a string representation of the contents of the specified int array. Also, to use it you will have to use :

“Arrays.toString(int [])”方法返回指定int数组内容的字符串表示形式。此外,要使用它,您将不得不使用:

    import java.util.Arrays

The output will be like this :

输出将是这样的:

    [1,2,3,4,5,6,7,8,9,10,..............,28122,28123]

I hope this helps.

我希望这有帮助。

#8


0  

  • First line : array created.
  • 第一行:创建数组。
  • Third line loop started from j = 1 to j = 28123
  • 第三行循环从j = 1开始到j = 28123
  • Fourth line you give the variable x value (0) each time the loop is accessed.
  • 第四行,每次访问循环时都给出变量x值(0)。
  • Fifth line you put the value of j in the index 0.
  • 第五行,你将j的值放在索引0中。
  • Sixth line you do increment to the value x by 1.(but it will be reset to 0 at line 4)
  • 第六行你将x增加到1.(但它将在第4行重置为0)

#9


0  

public class Array {
    static int a[] = new int[101];
    int counter = 0;
    public int add(int num) {
        if (num <= 100) {
            Array.a[this.counter] = num;
            System.out.println(a[this.counter]);
            this.counter++;

            return add(num + 1);
        }
        return 0;
    }

    public static void main(String[] args) {
        Array c = new Array();
        c.add(1);
    }
}