1

因此,我在帮助下解决了大部分问题,但是现在当我尝试正确打印 IntegerSet 时,它并没有改变。

我不确定为什么会出现这种类型的问题,可能是因为测试很糟糕?

This is how it remains. 

setA: IntegerSet@1893c911  // Any ideas why this happens?
setB: IntegerSet@e7587b2   // And this?
1) insertElement into setA
2) deleteElement from setA
3) insertElement into setB
4) deleteElement from setB
5) intersection of setA and setB
6) union of setA and setB
7) equality of setA and setB
Select from the menu above (or 0 to exit): 

这是类代码:

int [] a;  // holds a set of numbers from 0 - 100

          public IntegerSet () {
            // an empty set, all a[i] are set to 0
            a = new int [101];
          }

          // A constructor that copies from an existing set.
          public IntegerSet (IntegerSet existingSet) {
            a = new int [101];
            for(int i=0; i<a.length; i++)
              a[i] = existingSet.a[i];
          }

          public void deleteElement(int i) {
            if ((i >= 0) && (i < a.length))
              a[i] = 0;  // set to 1
          }

          public void insertElement(int i) {
            if ((i >= 0) && (i < a.length))
              a[i] = 1;  // set to 1
          }

          public boolean isSet(int i) {
            return (a[i] == 1);
          }

          public int lengthOfArray(){
              return this.a.length;
        }

          public static IntegerSet union(IntegerSet otherSet, IntegerSet nextSet) {

                for(int i=0; i<otherSet.length(); i++) {
                  if (otherSet.isSet(i))
                    nextSet.insertElement(i);
                }

                return nextSet;
              }

              public static IntegerSet intersection(IntegerSet otherSet, IntegerSet nextSet) {

                for(int i=0; i<otherSet.length(); i++) {
                  if (!otherSet.isSet(i))
                    nextSet.deleteElement(i);
                }

                return nextSet;
              }



          // return true if the set has no elements
          public boolean isEmpty() {
            for (int i=0; i<a.length; i++)
              if (isSet(i)) return false;
            return true;
          }

          // return the 'length' of a set
          public int length() {
            int count = 0;
            for (int i=0; i<a.length; i++)
              if (isSet(i))
                count++;
            return count;
          }

          // Print a set to System.out
          public void setPrint() {
            System.out.print("[Set:");

            if (isEmpty())
              System.out.print("---");

            for (int i=0; i<a.length; i++) {
              if (isSet(i))
                System.out.print(" " + i);
            }

            System.out.print("]\n");
          }

          // return true if two sets are equal
          public boolean isEqualTo(IntegerSet otherSet) {
            for(int i=0; i<a.length; i++) {
              if (otherSet.isSet(i) != isSet(i))
                return false;
            }
            return true;
          }

测试代码:

import java.util.Scanner;

public class IntegerSetTest {

    public static void main(String args[]) {

        IntegerSet setA = new IntegerSet();
        IntegerSet setB = new IntegerSet();

        Scanner scan = new Scanner(System.in);
        int input;

        do {            
            // Just for formatting purposes...
            System.out.println();            
            System.out.println("setA: " + setA);
            System.out.println("setB: " + setB);
            System.out.println("1) insertElement into setA");
            System.out.println("2) deleteElement from setA");
            System.out.println("3) insertElement into setB");
            System.out.println("4) deleteElement from setB");
            System.out.println("5) intersection of setA and setB");
            System.out.println("6) union of setA and setB");
            System.out.println("7) equality of setA and setB");
            System.out.println("Select from the menu above (or 0 to exit): ");
            input = scan.nextInt();

            switch(input) {
                case 1: 
                    System.out.print("Enter an element to insert into setA: ");
                    setA.insertElement(scan.nextInt());
                    break;
                case 2:
                    System.out.print("Enter an element to delete from setA: ");
                    setA.deleteElement(scan.nextInt());                  
                    break;
                case 3:    
                    System.out.print("Enter an element to insert into setB: ");
                    setB.insertElement(scan.nextInt());                 
                    break;
                case 4:
                    System.out.print("Enter an element to delete from setB: ");
                    setB.deleteElement(scan.nextInt());                  
                    break;
                case 5:    
                    System.out.println("The intersection of setA and setB is: " + IntegerSet.intersection(setA, setB));
                    break;
                case 6:
                    System.out.println("The union of setA and setB is: " + IntegerSet.union(setA, setB));
                    break;
                case 7:
                    System.out.println("setA and setB are " + (setA.isEqualTo(setB) ? "" : "un") + "equal");
                    break;
                default:
                    if (input != 0) {
                        System.out.println("\n*** Error, invalid input! ***\n");
                    }
            }          
        }while(input != 0);
    }

}
4

3 回答 3

0

当您尝试输出对象时...

System.out.println("setA: " + setA);

您正在尝试将对象转换为字符串。为了做到这一点:

字符串转换是通过 toString 方法实现的,由 Object 定义并由 Java 中的所有类继承。( docs.oracle.com )

...您需要覆盖类中的toString(...)方法IntegerSet

    public String toString() {
        // Maybe you should use java.lang.StringBuilder instead
        String returnValue = "[Set:";

        if (isEmpty())
          returnValue += "---";

        for (int i=0; i<a.length; i++) {
          if (isSet(i))
            returnValue += " " + i;
        }

        returnValue += "]\n";

        return returnValue;
    }
于 2012-12-04T06:36:49.653 回答
0

您需要编写一个toString()方法来IntegerSet创建一个String您想要的对象表示形式。toString()打印您所看到的字符串类型的默认实现。

于 2012-12-04T06:36:57.507 回答
0
IntegerSet@1893c911  // Any ideas why this happens?

这表明您的 IntegerSet 类没有覆盖 toString() 方法。所以它使用默认的 toString 实现来打印类和实例标识符。

您需要覆盖 toString 方法。Arrays.toString() 有助于很好地打印数组。

于 2012-12-04T06:37:04.240 回答