首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义BitString类中的Java方法

自定义BitString类中的Java方法
EN

Stack Overflow用户
提问于 2014-09-18 00:13:15
回答 2查看 1.2K关注 0票数 2

对于赋值,我们应该修改自定义的BitString类。有超过10个函数,我们需要实际编写代码,我被困在第一个。这是类的开头部分,以及我试图使用的一些方法:

代码语言:javascript
复制
public class BitString implements Cloneable {
    // An array to hold the bits that make up the bit string.
    private boolean bits[];
    /**
     * A constant that defines the size of the default bit string.
     */
    public static final int DEFAULT_SIZE = 8;

    /**
     * Creates a new, all false, bit string of the given size.
     */
    public BitString(int size) {
        if (size < 1) throw new IllegalArgumentException("Size must be positive");
        bits = new boolean[size];
    }

    /**
     * Creates a new all false bit string of size DEFAULT_SIZE.
     */
     public BitString() {
         this(DEFAULT_SIZE);
     }

     /**
      * Set the value of a bit string at the given index to true.
      */
     public void set(int index) {
         bits[index] = true;
     }

     /**
      * Set the value of a bit string at the given index to false.
      */
     public void clear(int index) {
         bits[index] = false;
     }

下面是我正在处理的方法(所给出的唯一部分是方法和输入类型),我不能调用bits.set()bits.clear()或他们正在做的相同的操作。在编译时我得到

错误:无法对非静态字段位进行静态引用。

在这两个方法调用中。

代码语言:javascript
复制
public static BitString decimalToUnsigned(int n, int size) {
    //throw new UnsupportedOperationException("This function needs to be completed!");
    int result = 0;
    int multiplier = 1;
    int base = 2;

    while(n > 0) {
        int remainder = n % base;
        n = n / base;
        if (remainder == 0) {
            //value = false;
            try {
                //bits.clear(size);
                bits[size] = false;
            } catch (InsufficientNumberOfBitsException ie) {}
        } else {
            //value = true;
            try {
                //bits.set(size);
                bits[size] = true;
            } catch (InsufficientNumberOfBitsException ie) {}
        }
        result = result + remainder * multiplier;
        multiplier = multiplier * 10;
        size--;
    }

    System.out.println("Result..." + result);

    return(bits);
} 

谢谢你的帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-18 01:00:58

我们必须在这里做一些假设:例如,静态方法是BitString上的一个方法。

在这种情况下,该方法显然应该创建一个BitString对象,因为它返回一个对象。因此,它应该为您正在处理的参数创建一个所需的大小。由于您有不允许调用set和clear方法的(任意的、有点愚蠢的)限制,因此需要从直接创建的BitString中访问BitString变量;因为静态方法位于BitString类上,所以可以这样做:

代码语言:javascript
复制
public static BitString decimalToUnsigned(int n, int size)
{
  // ...
  BitString bitString = new BitString(size);
  // ... loops, logic, etc. all to be put in here; when you're ready to
  // access the bits array, use:
  bitString.bits[index] = false;
  // ...
  // then when you're ready to return your BitString object, just:
  return bitString;
}

是的,位被声明为私有,但这仅仅意味着不能从类外部访问它。静态方法在类中,但它不能使用成员变量,因为静态方法不操作实例(除了它创建的实例)。

看看这是否能让您通过编译错误,并继续到您的逻辑。

附注:我不认为这是一个很好的任务,它会让你对静态和非静态的方法感兴趣,但我认为有更好的方法来做到这一点。说你必须使用和返回一个类,但是你不能调用它的方法,这几乎不是一个真实的场景。

票数 1
EN

Stack Overflow用户

发布于 2014-09-18 01:50:59

在静态方法中,您需要一个BitString实例来放置您的vales。我就是这样做的:

代码语言:javascript
复制
public class BitString implements Cloneable {

  /** A constant that defines the size of the default bit string. */
  public static final int DEFAULT_SIZE = 8;

  // an array to hold the bits that make up the bit string
  private boolean bits[];

  /** Creates a new, all false, bit string of the given size. */
  public BitString(int size) {
    if (size < 1) {
      throw new IllegalArgumentException("size must be positive");
    }
    bits = new boolean[size];
  }

  /** Creates a new all false bit string of size DEFAULT_SIZE. */
  public BitString() {
    this(DEFAULT_SIZE);
  }

  /** Set the value of a bit string at the given index to true. */
  public void set(int index) { // might want to check index bounds
    bits[index] = true;
  }

  /** Set the value of a bit string at the given index to false. */
  public void clear(int index) { // might want to check index bounds
    bits[index] = false;
  }

  public String toString() { // one possible implementation, might not want to add leading 0's
    StringBuilder buf = new StringBuilder(bits.length);
    for (Boolean bit : bits) {
      buf.append(bit ? '1' : '0');
    }
    return buf.toString();
  }

  public static BitString decimalToUnsigned(int n, int size) {
    // throw new UnsupportedOperationException("this function needs to be completed");
    // might want to check that size is big enough

    // this is the key here: you need an instance of the object that has the bits array inside it
    BitString result = new BitString(size);
    while (n != 0 && size > 0) {
      size--; // use size to index into the BitString
      if ((n & 1) == 1) { // % 2 doesn't work well with negative numbers, you have to worry about +-1 then
        result.set(size); // set bit if needed
      }
      n = n >>> 1; // unsigned shift to the next bit
    }
    return result;
  }

  public static void main(String[] args) {
    // can be invoked with just decimalToUnsigned(42, 10) but I want to make it more clear
    BitString example1 = BitString.decimalToUnsigned(42, 10);
    System.out.println(example1);

    BitString example2 = BitString.decimalToUnsigned(-42, 10); // will treat -42 as unsigned
    System.out.println(example2);

    BitString example3 = BitString.decimalToUnsigned(-1, 33); // will treat -1 as unsigned giving 32 1's
    System.out.println(example3);
  }
}

它打印:

0000101010 1111010110 011111111111111111111111111111111

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25902289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档