简体   繁体   English

这个Java程序怎么了?

[英]What's wrong with this Java program?

class Main {
    public static void main(String[] args) {
        int x = 3;
        int [] a = new int [x];
        x+=5;
        System.out.println((x+=5));
        System.out.println(a.length);
    }
}

Why doesn't it effect the length of array "a" ? 为什么它不影响数组“ a”的长度? What am i doing wrong? 我究竟做错了什么?

The thing is I was asked to add 5 to the length of an array using a loop. 问题是我被要求使用循环将5加到数组的长度上。 I can't even increase the length directly. 我什至不能直接增加长度。 It looks like a simple issue but I'm still in learning process. 看起来很简单,但是我仍在学习中。 I did it yesterday but I don't remember how. 我昨天做了,但我不记得怎么做。

You are just changing the value of x, you also have to reassign this value to change the array length. 您只是在更改x的值,还必须重新分配该值以更改数组长度。

        int x = 3;
        int[] a = new int[x];
        x += 5;
        a = new int[x];
        System.out.println(a.length);

Merely changing value of x is not going to help, 仅仅改变x的值将无济于事,

You need top explicitly assign modified value back in array 您需要顶部在数组中显式分配修改后的值

You cannot increase the size of an array directly. 您不能直接增加数组的大小。 You will learn about ArrayList s later. 稍后您将学习ArrayList You can reassign an array variable to a new array; 您可以将数组变量重新分配给新数组。 the length is not part of the array type. 长度不是数组类型的一部分。 So, let's break this up into steps. 因此,让我们将其分解为几个步骤。

  1. Create an array of size 3. 创建一个大小为3的数组。
  2. Report to the user the contents of the old array. 向用户报告旧数组的内容。
  3. Store the length in a variable. 将长度存储在变量中。
  4. Add 5 to that variable. 5加到该变量。
  5. Create a new array with the new length. 用新的长度创建一个新的数组。
  6. Copy the elements of the old array to the corresponding positions in the new array using a for loop, though System.arrayCopy is shorter. 尽管System.arrayCopy较短,但使用for循环将旧数组的元素复制到新数组中的相应位置。
  7. Assign the new array to the old array variable. 将新数组分配给旧数组变量。
  8. Report to the user the contents of the changed old array. 向用户报告已更改的旧阵列的内容。
  9. Look up the List and ArrayList documentation so you see how people do this in practice. 查找ListArrayList文档,以便了解人们在实践中是如何做到的。

When you work on exercises like this, always break it down into individual steps first. 当您进行此类练习时,请始终先将其分解为各个步骤。 When you program, break your methods into individual methods that do just one thing. 当您编程时,将您的方法分解为只做一件事的单个方法。

Ok, I sum up to give you a more complete answer: 好吧,我总结一下给您一个更完整的答案:

First, when you initialize an array, you assign a defined/finite chunk of memory to it. 首先,在初始化数组时,将为其分配一个已定义/有限的内存块。 If you want to change the length of such an array in Java, you need to initialize a new one. 如果要在Java中更改此类数组的长度,则需要初始化一个新数组。 There are no retrospective automatic reinitialization of arrays in Java. Java中没有追溯自动重新初始化数组的方法。 Other languages might do something something similar for you, but Java doesn't. 其他语言可能会为您做类似的事情,而Java则不会。

Second, you pass the x in your array-initialization by value which means, the functionalities, which create your array, receive the value ones but not the memory address. 其次, 您通过值在数组初始化中传递x这意味着创建数组的功能将接收值1,但不接收内存地址。 Methods in Java do it likewise: You pass values as parameters to them but they cannot see the variables directly as long as you don't explicitly reference to them in your local environment of each method. Java中的方法也是如此:您将值作为参数传递给它们,但是只要您没有在每种方法的本地环境中显式引用它们,它们就不能直接看到变量。 This is why no function, which accepts parameters, is directly influenced by changes of the variables from which values have been passed. 这就是为什么没有任何接受参数的函数直接受传递值的变量的变化影响的原因。

Third, if you try to change the size of an array often, you should consider using ArrayLists which include the functionality which you are looking for. 第三,如果您尝试经常更改数组的大小,则应考虑使用包含所需功能的ArrayLists

Forth, if you want to keep using arrays instead of something else and want to keep the array members/values, you still need to pass the old array content to a new initialized array . 第四,如果您想继续使用数组而不是其他数组,并且想要保留数组成员/值,则仍然需要将旧的数组内容传递给新的初始化数组 This can be achieved by looping through the old array. 这可以通过遍历旧数组来实现。

First of all, what are you trying to achieve? 首先,您要达到什么目标? The length of your will be equal to the x value during the array creation (second line at your code), if you want to use dynamic array you should consider to use java.util.ArrayList . 您的长度将等于数组创建过程中的x值(代码的第二行),如果要使用动态数组,则应考虑使用java.util.ArrayList

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

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