Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 程序输出整数的十六进制而不需要任何输入?_Java - Fatal编程技术网

Java 程序输出整数的十六进制而不需要任何输入?

Java 程序输出整数的十六进制而不需要任何输入?,java,Java,因此,我在帮助下解决了这方面的大部分问题,但是现在当我尝试正确打印整数集时,它不会改变 我不确定为什么会出现这种问题,可能是因为测试很糟糕吧 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

因此,我在帮助下解决了这方面的大部分问题,但是现在当我尝试正确打印整数集时,它不会改变

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

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

当您尝试输出对象时

System.out.println("setA: " + setA);
您正在尝试将对象转换为字符串。为此:

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

。。。您需要重写类内的
整数集

    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;
    }
公共字符串toString(){
//也许您应该改用java.lang.StringBuilder
字符串returnValue=“[Set:”;
if(isEmpty())
returnValue+=“--”;

对于(int i=0;i您需要为
IntegerSet
编写一个
toString()
方法,该方法创建一个
String
,并使用您想要的对象表示形式。默认的
toString()
实现会打印您看到的字符串类型

IntegerSet@1893c911  // Any ideas why this happens?
这表明youre IntegerSet类不重写toString()方法,因此它使用默认的toString实现来打印类和实例标识符

您需要重写toString方法。Arrays.toString()可以很好地打印数组

IntegerSet@1893c911  // Any ideas why this happens?