【问题标题】:Having trouble working with a class with an array在使用带有数组的类时遇到问题
【发布时间】:2012-06-29 18:41:46
【问题描述】:

当我调用我的 TruthTable 类并使用输入填充它时,当我尝试设置与门的输入时,我无法访问数组中的各个插槽?

threeAndGates.java - 发生错误的类

import java.util.Scanner;

public class threeAndGates {

   public static void main(String[] args){
      LogicGate and1 = new LogicGate(LogicGate.AND);
      LogicGate and2 = new LogicGate(LogicGate.AND);
      LogicGate and3 = new LogicGate(LogicGate.AND);

      System.out.print("What is the number of Inputs? ");
      Scanner scan = new Scanner(System.in);
      int numOfInputs = scan.nextInt();

      System.out.print("What is the number of Outputs? ");
      int numOfOutputs = scan.nextInt();

      TruthTable Table1 = new TruthTable(numOfInputs,numOfOutputs);
      Table1.PopulateTruthTable();

      //below is where it is giving me "the type of the expression must be an array type but it resolves to TruthTable"
      for(int r = 0; r<(Math.pow(2, numOfInputs)) ; r++ ){
         and1.setInput1(Table1[r][0]);
         and1.setInput2(Table1[r][1]);
         and2.setInput1(Truth1[r][2]);
         and2.setInput2(Truth1[r][3]);
         and3.setInput1(and1.getOutput());
         and3.setInput2(and2.getOutput());

         Table1[r][numOfInputs + numOfOutputs] = and3.getOutput();
      }

      Table1.printTruthTable();
   }
}

TruthTable.java

public class TruthTable {
   private int numOfInputs;
   private boolean[][] table;

   public TruthTable(int inputs, int outputs){
      this.numOfInputs = inputs;
      int rows = (int) Math.pow(2,inputs);
      int columns =  inputs + outputs;
      table = new boolean[rows][columns];
   }

   public void printTruthTable(){
      for(int r = 0 ; r < table.length ; r++){
         for(int c = 0; c < table[r].length; c++)
            System.out.printf("%-5b ", table[r][c]);
         System.out.println();
      }
   }

   public String toString(){
      String outStr = new String();
      for(int r = 0; r < table.length; r++){
         for(int c = 0; c < table[r].length; c++)
            outStr += String.format("%-5b ", table[r][c]);
         outStr += '\n';
      }

      return outStr;
   }

   public boolean[][] PopulateTruthTable(){
      String s; 
      String r ="";
      int[] Line = new int[numOfInputs];
      boolean bit;

      for ( int i= 0; i < Math.pow(2,numOfInputs) ; i++){
         int x = numOfInputs - Integer.toBinaryString(i).length();
         for(int j = 0; j<x ; j++)
            r += "0"; 
         s = r + Integer.toBinaryString(i);
         for(int k=0; k<s.length() ;k++){
           Line[k] = s.charAt(k)-48;
         }  

         for(int m=0 ; m<numOfInputs ; m++){    
           if(Line[m]==1) bit = true;
           else bit = false; 
           table[i][m] = bit;
         }
         r="";
      } 
      return table;
   }
}

【问题讨论】:

    标签: java oop class object


    【解决方案1】:

    您的 TruthTable 类不是数组。它包含一个数组。你可以在你的 TruthTable 类中添加一个 get 和 set 方法:

    public boolean getValueAt(int x, int y) {
       return this.table[x][y];
    }
    
    public void setValueAt(int x, int y, boolean value) {
       this.table[x][y] = value;
    }
    

    并使用它来处理 TruthTable 值。


    这与您的问题无关,但是在您的类中命名变量时,一般做法是使用小写。例如你有:

    TruthTable Table1 = new TruthTable(numOfInputs,numOfOutputs);
    

    会更好

    TruthTable table1 = new TruthTable(numOfInputs,numOfOutputs);
    

    可能是最好的

    TruthTable truthTable = new TruthTable(numOfInputs,numOfOutputs);
    

    你命名的东西越好,越一致,就越容易阅读。

    【讨论】:

    • +! - 命名约定的好提示!它不仅可以帮助您将来阅读自己的代码,还可以帮助我们 SO 用户更快地帮助您解决问题,因为我们也可以更快地理解您的代码!
    • 只是代码高亮显示正确让我更容易处理,我可能还应该注意到类名也应该以大写开头,但我认为我的帖子已经很长了。
    • 我知道你的意思。甚至方法名也是大写的:Table1.PopulateTruthTable(); :(
    【解决方案2】:

    您的 TruthTable 类不是多维数组;它有一个多维数组字段。因此,您不能使用以下语法:

    tableInstance[x][y]
    

    如果你的 TruthTable 的表字段是公开的,或者更好的是,它有一个 getter,你可以做这样的事情来代替......

    tableInstance.getTable()[x][y]
    

    某些语言(如 C#)还支持运算符重载,这将允许您定义使用 [] 索引运算符(或其他如 +/ 等)的行为。这将允许您使索引工作。不幸的是,Java 没有这个功能。

    【讨论】:

      【解决方案3】:

      这更像是一个评论而不是一个答案,但我需要更多空间。

      如果您的代码稍后给您带来问题,我可以提出建议吗?将 populateTruthTable 分解为 2 或 3 个方法,将每个循环放在它自己的命名方法中,因为 Each method should do exactly one thing

      另外,您可能不应该直接从主类访问数组,而是将主类“for”循环中的所有代码放入 TruthTable 类中的一个方法中,并从 main 调用该方法,因为您应该 @987654322 @

      我并不是要说你做错了什么,你显然做得很好,但随着你的进步,学习更多的编码技巧/练习总是好的,你看起来就像你在这些会派上用场的水平。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多