因此,我在帮助下解决了大多数问题,但是现在当我尝试正确打印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);
    }

}

最佳答案

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

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


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


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


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

    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;
    }

10-06 16:02