【问题标题】:Accessing and filling array(s) by its name in the main method of a java class (created via member function )在 java 类的 main 方法中按数组名称访问和填充数组(通过成员函数创建)
【发布时间】:2020-01-11 00:11:44
【问题描述】:

我有一个程序,我应该在其中创建一个类,该类具有一个创建不同简单数据类型数组的成员函数。我在 main 方法中实例化一个类,即创建这个类的一个对象,然后调用 createArray 方法。当我尝试使用Arrays.fill(name,startIndex, endIndex, value) 填充具有相同类型数据的数组时,它找不到这样名称的数组。我想问你这是为什么?

import java.lang.reflect.Array;
import java.util.Arrays;

    public class MainClass {
      public void createArray(){
      int [] ints = new int[10];
      }
    }
    class Test{
        public static void main(String[] args) {
            MainClass mcl = new MainClass();
            mcl.createArray();
            for (int i = 0;i<10;i++){
                //cannot resolve symbol ints - why?
                Arrays.fill(ints,0,9,8);
            }
        }
    } 

【问题讨论】:

    标签: java arrays oop element


    【解决方案1】:

    您的代码存在一些问题,下面提供了一个更简单的版本:

    public class MainClass {
      public void createArray(){
           //This lives only in here. It is not available outside.
           int [] ints = new int[10];
      }
    }
    class Test{
        public static void main(String[] args) {
            MainClass mcl = new MainClass();
            mcl.createArray();
            for (int i = 0;i<10;i++){
                //cannot resolve symbol ints - why?
                Arrays.fill(ints,0,9,8);
            }
        }
    }
    

    在您创建的代码中,ints 仅存在于 createArray 方法中。一旦执行退出此方法,此数组将不再可用并标记为垃圾回收。

    要解决这个问题,您需要将您的方法签名更改为return 一个int 类型的数组(这样当您调用createArray 方法时,此方法会返回它创建的数组)。然后,您需要确保 捕获 在主类中返回的数组。这将允许您对其执行操作。

    【讨论】:

      【解决方案2】:

      如果您不想更改createArray 方法签名,您可以将数组存储在类属性中并为其创建getter 方法。 这是一些代码:

      import java.util.Arrays;
      
      class MainClass {
      
          private int[] ints;
      
          public void createArray(){
              this.ints = new int[10];
          }
      
          public int[] getInts() {
              return ints;
          }
      }
      class Test{
          public static void main(String[] args) {
              MainClass mcl = new MainClass();
              mcl.createArray();
              for (int i = 0;i<10;i++){
                  //cannot resolve symbol ints - why?
                  Arrays.fill(mcl.getInts(),0,9,8);
              }
          }
      }
      

      但由于createArray 已经公开,更好的方法可能是更改其签名以返回int[],该案例的示例代码:

      import java.util.Arrays;
      
      class MainClass {
      
          private int[] ints;
      
          public int[] createArray(){
              this.ints = new int[10];
              return ints;
          }
      
      }
      class Test{
          public static void main(String[] args) {
              MainClass mcl = new MainClass();
              mcl.createArray();
              for (int i = 0;i<10;i++){
                  //cannot resolve symbol ints - why?
                  Arrays.fill(mcl.createArray(),0,9,8);
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        int 变量仅在createArray 范围内定义,不在外部。如果你在外面需要它,你应该把它归还。

        这里有一些关于Java variable scope的讲座

        祝你好运。

        【讨论】:

          【解决方案4】:
          1. ints 不在主类的范围内,它在创建数组中。你可以这样做:
          class MainClass {
            int [] ints = new int[10];
          
            public void createArray()
            {int [] ints = new int[10]; }
          
          }
          public class Test{
              public static void main(String[] args) {
                  MainClass mcl = new MainClass();
                  mcl.createArray();
                  for (int i = 0;i<10;i++){
                      //mcl.ints not just ints
                      Arrays.fill(mcl.ints,0,9,8);
                  }
              }
          }
          
          
          1. 您必须公开 Test 类,因为它具有 main 方法并使用 Test.java 保存文件

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-02-14
            • 2020-04-24
            • 2017-09-19
            • 2022-12-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多