如何在 Java 中扩展数组而不更改其名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4463028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:50:57  来源:igfitidea点击:

How to extend an array in Java without changing its name

javaarraysextend

提问by Kajsa

I wonder if it's possible to extend an array in Java but without changing its name, since I have multiple methods linked to this array. I was thinking of creating a new array with the same name but twice as big, and then copy all elements from the first array to the second. Is this possible?
Basically I want to make an array with accounts at a bank, and if the customer creates so many accounds that the array doesn't have enough elements, it should extend itself.
Thank you for any replies!

我想知道是否可以在 Java 中扩展数组但不更改其名称,因为我有多个方法链接到这个数组。我正在考虑创建一个名称相同但大两倍的新数组,然后将第一个数组中的所有元素复制到第二个数组中。这可能吗?
基本上我想在银行创建一个带有帐户的数组,如果客户创建了太多的帐户以致该数组没有足够的元素,它应该扩展自己。
感谢您的任何回复!

回答by thejh

Use an ArrayList, it does all of that for you. Simply call add()or addAll()to append stuff.

使用ArrayList,它会为您完成所有这些。只需调用add()addAll()附加内容。

回答by khachik

You cannot change the array you have, but can create a new array with the desired type and size, copy data from the original array to the newly created, and then assign it to the original array variable.

您无法更改您拥有的数组,但可以创建一个具有所需类型和大小的新数组,将数据从原始数组复制到新创建的数组,然后将其分配给原始数组变量。

But you shouldn't do this.

但你不应该这样做。

Instead of an array use any implementation of java.util.List. ArrayListis a good choice for this case, I think.

使用java.util.List. ArrayList我认为对于这种情况是一个不错的选择。

回答by Nick

Here is a method that i wrote for my Java Programming Class

这是我为 Java 编程类编写的方法

private static double[] extendArraySize(double [] array){
    double [] temp = array.clone();
    array = new double[array.length + 1];
    System.arraycopy(temp, 0, array, 0, temp.length);
    return array;
}

It's pretty self explanitory on how it works, although on the way it's used it must have the array assigned to the method with the arrays name passing as the argument, and the data type of the method must always be the type of array passed in.

关于它是如何工作的,这是非常不言自明的,尽管在使用它的方式上,它必须将数组分配给方法,并将数组名称作为参数传递,并且方法的数据类型必须始终是传入的数组类型。

example:

例子:

double [] z = {1,2,3};  //size of z = 3; [0,1,2]   VALUES: [1,2,3]
z = extendArraySize(z); //size of z = 4; [0,1,2,3] VALUES: [1,2,3,0];

Sorry if it's explained terribly and i'm sure there's a better way of doing it VIA vectors or ArrayLists. But if you're going to want to extend a primitive array, this is one way of doing it.

对不起,如果它的解释很糟糕,我相信有更好的方法通过向量或 ArrayLists 来做到这一点。但是如果你想扩展一个原始数组,这是一种方法。

Hope I helped =D

希望我有所帮助 =D

回答by Sylvain Leroux

Even if using an ArrayListis probably a good advice in many circumstances, there are perfectly legitimate occasions for using plain old arrays.

即使ArrayList在许多情况下使用 an可能是一个很好的建议,也有使用普通旧数组的完全合法的场合。

In that case, if you need to resize your array, you might want to investigate one of the java.utils.Arrays.copyOfmethods. Please note however those won't really resizeyour array. They will merely create a new array and copy common items.

在这种情况下,如果您需要调整数组大小,您可能需要研究其中一种java.utils.Arrays.copyOf方法。但是请注意,那些不会真正调整数组的大小。他们只会创建一个新数组并复制公共项目。

If the new array has a size greater than the old one, the new items will be initialized to some default value (i.e.: falsefor boolean[], nullfor T[]-- see the documentation for details). You have to use those function like that:

如果新数组的大小大于旧数组,则新项将被初始化为某个默认值(即:falsefor boolean[]nullfor T[]-- 有关详细信息,请参阅文档)。您必须像这样使用这些功能:

myArray = copyOf(myArray, myNewSize); 

Remember however that this method will always return a newarray. Even if the requested size is the same as the original one. If this in notdesirable, you will have to write something like that:

但是请记住,此方法将始终返回一个数组。即使请求的大小与原始大小相同。如果这不是理想的,你将不得不写类似的东西:

myArray = (myNewSize > myArray.length) ? copyOf(myArray, myNewSize) : myArray;

回答by B48max

public static void extendarrayafterinitialization(int [] x){
        Scanner input =new Scanner(System.in);
        System.out.print("How many values do you need to insert to the Array : ");
        int how=input.nextInt();
        int length=x.length+how;
        int [] all=new int[length];
        int value=0;
        System.out.println(length);
        for(int i=0;i<x.length;i++){
            all[i]=x[i];
            }
            for(int j=x.length;j<length;j++){
                System.out.print("Enter new value : ");
                int index=input.nextInt();
                all[j]=index;
                }
        for(int print : all){
            System.out.println(print);
            }
        }