简体   繁体   English

Java数组问题

[英]Java Array Questions

So this is just a basic question. 因此,这只是一个基本问题。 I was working with arrays in Java, figured out what I needed to do for my little assignment. 我正在用Java处理数组,弄清楚我需要做些什么。 I decided to play around with the array and see what would happen if I print out my array. 我决定试一下该阵列,看看如果我打印出该阵列会发生什么。 I was very confused about the result I got with this code: 我对使用此代码得到的结果感到非常困惑:

public class array_output{
    public static void main(String[] args){
       int[] anArray = new int[10];
       for(int p = 0; p < 10; p++){
           anArray[p] = p;
           //System.out.print(anArray[p] + " ");
           p++;
       }
       System.out.println (java.util.Arrays.toString(anArray));
    }
}

So the original intension was to just make an array "0, 2, 4, 6, 8". 因此,最初的意图是仅使数组为“ 0、2、4、6、8”。 I decided to put my System.out.print outside of the for loop and print it out. 我决定将System.out.print放在for循环之外并打印出来。 The output I have got was 我得到的输出是

[0, 0, 2, 0, 4, 0, 6, 0, 8, 0]

Actually when I am writing this I thought that it is putting a "0" in place of the numbers that are not there between 0-9 like 0 is false and if the number shows it's in the array, not sure though. 实际上,当我写这篇文章时,我以为是在0到9之间的数字(例如0)处放置一个“ 0”,这是假的,如果该数字显示在数组中,则不确定。

If anyone could explain to me if that's what is happening here, and maybe explain how to print the values of the array outside of the for loop, I would greatly appreciate it 如果有人可以告诉我这是否正在发生,并且也许可以解释如何在for循环外打印数组的值,我将不胜感激

You're incrementing p twice at each iteration: 您在每次迭代中将p递增两次:

for(int p = 0; p < 10; p++) { // here
    anArray[p] = p;
    p++; // and here
}

Remove the p++ from the body of the loop. 从循环体中删除p++

If you want the array to contain 0, 2, 4, 6, ..., the body of the loop should be 如果希望数组包含0、2、4、6,...,则循环的主体应为

anArray[p] = p * 2;

ie initialize every element of the array with the its position multiplied by 2. 即用其位置乘以2初始化数组的每个元素。

You are incrementing p twice in each iteration of the for loop. for循环的每次迭代中,您将p递增两次。
Therefore only the even indices of the array (0,2,4,6,8) are assigned. 因此,仅分配数组的偶数索引(0,2,4,6,8)。

for(int p=0; p<10; p++){ // <-- first time here
    anArray[p]=p;
    //System.out.print(anArray[p] + " ");
    p++; // <-- second time here
}

If you remove one of them (makes more sense to remove the second p++ ) it will work as expected. 如果删除其中一个(删除第二个p++更有意义),它将按预期工作。

BTW, if the goal is to assign only even values to the array, you should assign 2*p . 顺便说一句,如果目标是仅分配偶数到数组,则应分配2*p

for(int p=0; p<10; p++){
    anArray[p]=2*p;
}

Your problem is, that you increment your loop variable p two times: One time in the regular for loop and a second time at the end of the for loop - therefore your index skips every two steps and the skipped array elements are not filled at all. 您的问题是,使循环变量p两次递增:一次在常规for循环中,第二次在for循环结束时-因此,索引每两步跳过一次,并且跳过的数组元素根本不被填充。

The solution is to iterate normally but place index * 2 at the current array position: 解决方案是正常迭代,但将index * 2放在当前数组位置:

public class array_output{
        public static void main(String []args){
        int[] anArray = new int[10];

        for(int p=0; p<10; p++){
            anArray[p]=p * 2;
        }

        System.out.println (java.util.Arrays.toString(anArray));
    }
}

I recommend taking a look at the Java language specification 我建议看一下Java语言规范

Quote: 引用:

Array components are unnamed variables that are created and initialized to default values (§4.12.5) whenever a new object that is an array is created (§10, §15.10). 数组组件是未命名的变量,每当创建一个新的数组对象(第10节,第15.10节)时,它们都会创建并初始化为默认值(第4.12.5节)。 The array components effectively cease to exist when the array is no longer referenced. 当不再引用数组时,数组组件实际上将不复存在。

In your case you are using int and those are initialized by default to 0. 在您的情况下,您使用的是int并且默认情况下将其初始化为0。

That is the reason you get: [0, 0, 2, 0, 4, 0, 6, 0, 8, 0] Since the "missing" values are actually just not changed from their default value. 这就是您得到的原因:[0、0、2、0、4、0、6、0、8、0]因为“缺失”值实际上只是从其默认值不变。 The 2 4 6 8 is because you increment p twice, but I'm not sure if that is by design or if that's a bug since your initial question seems to indicate the former. 2 4 6 8是因为您将p递增了两次,但是我不确定这是设计使然还是错误,因为您的最初问题似乎表明前者。

And if you just want it to contain: 0 2 4 6 8 You can use the following code: 如果只希望包含以下内容:0 2 4 6 8您可以使用以下代码:

public class array_output{
    public static void main(String[] args){
       int[] anArray = new int[5];
       for(int p = 0; p < 5; p++){
           anArray[p] = p * 2;
       }
       System.out.println (java.util.Arrays.toString(anArray));
    }
}

There's no reason to init the array to size 10 if you only want it to contain 5 elements. 如果只希望包含5个元素,则没有理由将数组初始化为10号。

Hi Nothing to worry on this, when you have declared array like: 嗨,当您声明如下数组时,无需担心:

int[] anArray = new int[10];

It got initilized '0' in all the index start from 0 to 9; 从0到9的所有索引都将其初始化为“ 0”;

when you are inserting value it is inserting in: 当您插入值时,它将插入:

anArray[0]=0;
anArray[2]=2;
anArray[4]=4;
anArray[6]=6;
anArray[8]=8;

And since in all other index value is already '0'. 并且由于所有其他索引值已经为“ 0”。 So, if you print the array it will print like: 因此,如果您打印数组,它将打印为:

[0, 0, 2, 0, 4, 0, 6, 0, 8, 0]

If you want to print array just run a for loop and print as: 如果要打印数组,只需运行for循环并打印为:

for(int i=0;i<10;i++)
  System.out.println(anArray[i]);

By default, the int data type is initialized with the value 0. Since you increment p twice (once in the for loop, once in the body of the loop), only the elements with even indices get to have their value changed. 默认情况下,int数据类型初始化为值0。由于您将p递增两次(在for循环中一次,在循环体中一次),所以只有具有偶数索引的元素才能更改其值。

So in the first iteration you get anArray[0] = 0. Then you increment p twice so in the second iteration p is equal to 2 and you do anArray[2] = 2, while anArray[1] was skipped. 因此,在第一次迭代中,您将获得anArray [0] =0。然后您将p递增两次,因此在第二次迭代中,p等于2,然后执行anArray [2] = 2,而跳过anArray [1]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM