简体   繁体   English

更改Java中数组的大小

[英]Changing size of array in Java

I am trying to change the size of a String array, but I don't know what else to try and this method isn't working. 我正在尝试更改String数组的大小,但是我不知道还要尝试什么,并且此方法不起作用。 Here is my code: 这是我的代码:

import java.util.Scanner;

public class test{
    public static void main(String[] args){
        String[] names;
        names = new String[5];
        for(int i=0; i<10; ++i){
            addToArray(names, i, "blah");
        }
        System.out.println(names.length);
        System.out.println("bye");
    }

    public static void addToArray(String[] names, int i, String name){
        if (i < names.length){
            names[i] = name;
        }
        else {
            String[] temp;
            temp = new String[names.length + 10];
            System.arraycopy(names, 0, temp, 0, names.length);
            addToArray(temp, i, name);
            names = temp;
        }
    }
}

My problem is that when I run it, it prints that the final length of my array is 5, when it should say 15. 我的问题是,当我运行它时,它打印出数组的最终长度是5,应该说15。

The problem is, you can't change the reference of any of your ( Object ) method arguments, so... 问题是,您不能更改任何( Object )方法参数的引用,所以...

names = temp;

Only changes the reference of the names variable within the context of the addToArray method. 仅在addToArray方法的上下文中更改names变量的引用。 You should use a return statement instead... 您应该改用return语句...

public static String[] addToArray(String[] names, int i, String name){
    if (i < names.length){
        names[i] = name;
    }
    else {
        String[] temp;
        temp = new String[names.length + 10];
        System.arraycopy(names, 0, temp, 0, names.length);
        names = addToArray(temp, i, name);
    }
    return names;
}

Don't forget to maintain the reference once the method returns... 方法返回后,不要忘记维护引用。

for(int i=0; i<10; ++i){
    names = addToArray(names, i, "blah");
}

Once an array has been created, the size of that array cannot be changed. 创建阵列后,无法更改该阵列的大小。 An array can hold a fixed amount of objects once created. 数组一旦创建便可以容纳固定数量的对象。

An ArrayList however is able to change the amount of objects it holds dynamically. 但是, ArrayList能够动态更改其持有的对象数量。 A way to initialize a simple ArrayList would be: 初始化简单ArrayList方法是:

ArrayList<String> stringArrayList = new ArrayList<>();

Three points 三分

  1. Arrays are special objects in java, they have a simple attribute named length which is final . 数组是Java中的特殊对象,它们具有一个简单的名为length属性,该属性为final
  2. Also you are creating a new array, not re-sizing the original array. 另外,您正在创建一个新数组,而不是重新调整原始数组的大小。
  3. you can't change the reference of any of your method arguments 您不能更改任何方法参数的引用

change the return type as String[] and return names in addToArray() method and assign names= addToArray(String[] names, int i, String name) ; 将返回类型更改为String []并在addToArray()方法中返回名称,并分配names = addToArray(String [] names,int i,String name) ; in mainMethod thus you can maintain the keep tracking with reference when size increases mainMethod中,因此当大小增加时,您可以与参考保持跟踪

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

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