【问题标题】:Saving the value of an array in a method在方法中保存数组的值
【发布时间】:2012-09-18 00:10:39
【问题描述】:

我只是贴出伪代码,希望你能理解:

导入 java.util*.;

主要方法{

submethod1();

submethod1() {

 Screen input = new Scanner(System.in);
 int buy = input.nextInt();

  if( buy != 0) {
     submethod2();
    }

 submethod2() {
 Screen input = new Scanner(System.in);
 int[][]grid = new int [5][6]
 int row = input.nextInt();
 int col = input.nextint();
 grid[row][col] = 1;

假设我这次输入 1 代表行,1 代表列。然后grid[1][1] = 1。我想保存grid[1][1] 的值,以便下次我输入第 2 行第 2 列时:

 grid[1][1] = 1;
 grid[2][2] = 1; and so on for whatever row-col combination I type.

最后我想回到 submethod1,我想让 submethod1 理解 grid[1][1] = 1 并且 grid[2][2] 也有值 1;等等……

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

    处理此类问题的最佳方法是使用面向对象的方法。请记住,这就是我们使用 Java 的原因。

    创建一个类GridItem,它将具有三个属性rowcolumnvalue。当您存储一些 GridItem 的值创建对象并将其存储在全局列表中时。然后,您可以在任何函数中对其进行迭代并访问存储了哪些值。

         class GridItem
        {
            int row;
            int column;
            int value;
    
            public GridItem(int row, int column, int value)
            {
                this.row = row;
                this.column = column;
                this.value = value;
            }
            //Provide getters only
        }
        ArrayList<GridItem>items = new ArrayList<GridItem>();
        items.add(new GridItem(1, 1, 1));// 1 row 1 col 1 value
        items.add(new GridItem(2, 2, 2));// 2 row 2 col 2 value
    
    
        items.get(0).getRow()// get first 
    

    还有其他几种解决方案。例如维护一个全局数组,然后扩展它。创建网格列表等,但它们都很复杂,而且做得更多。

    【讨论】:

      【解决方案2】:

      这是一个范围界定问题。本质上,您每次调用submethod2() 时都会创建一个名为grid 的新int[][] 变量。要么将其存储为类变量,要么将其传入然后从submethod2() 返回并自己手动更新(我不推荐这种方法)

      如果没有更多上下文,很难推荐如何将您的问题分解为objects,但一种解决方案可能类似于以下内容:

      import java.util*.;
      
      public class MainClass {
          private int[][] grid;
      
          public static void main(String[] args) {
              submethod1();
          }
      
          private void submethod1() {
              grid = new int[5][6];
              Screen input = new Scanner(System.in);
              int buy = input.nextInt();
      
              if( buy != 0) {
                  submethod2();
              }
          }
      
          private void submethod2() {
              Screen input = new Scanner(System.in);
              int row = input.nextInt();
              int col = input.nextint();
              grid[row][col] = 1;
          }
      }
      

      【讨论】:

        【解决方案3】:

        下面我假设您询问的是在程序实例中而不是在程序调用的各个实例之间保存网格的值。如果您想在各种程序调用之间保存网格值,则必须将网格值存储在某些文件等中。

        不是在 submethod2() 中创建数组网格,而是将其创建为类变量,并将 submethod1()、submethod2() 创建为成员函数。

        在 main 方法中创建一个对象并在该对象上调用 submethod1()

        类似

        class ABC
        {
        int[][] grid = new int[5][6];
        
         submethod1()
        {
        ...
        }
        
         submethod2()
        {
         ...
         }
        
        
        public static void main(String args[])
        {
          ABC abc = new ABC();
          abc.submethod1();
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-27
          • 2023-03-14
          • 1970-01-01
          • 2020-12-04
          • 1970-01-01
          • 2021-11-02
          • 2020-09-29
          • 1970-01-01
          相关资源
          最近更新 更多