2015-10-15 126 views
-3

我知道这是像我这样的noobs一个相当常见的错误,但我似乎无法读懂任何我能理解的东西。对于我的作业,我们需要创建一个名为NumberList的long数组。我们需要完成的一个方法叫做toString,它明显地将数组转换为一个字符串来打印。Java错误:静态方法无法引用非静态方法

  1. 因此,在我的主要方法中,我简单地创建了一个空白的NumberList对象,并尝试在其上运行toString()方法。编译器说“不能从静态上下文中引用非静态方法toString()”。为什么!?
  2. 其次,我创建了一个long数组的测试数组,我想将其传递给我的NumberList对象。我调用了数组testExample并创建了一些任意值。但是,当我写

new NumberList(testExample);

我得到更多的错误。为什么我不能这样做?

终于这是我的代码。请忽略除main,构造函数和toString之外的所有方法。

谢谢你这么多

public class NumberList implements java.util.Collection { 

//instance stuff 
private long[] longArray; 

public static void main (String[] args) { 

    long[] testExample; 
    testExample = new long[3];  
    testExample[0] = 1; 
    testExample[1] = 2; 
    testExample[2] = 3; 

    new NumberList(); 
    //System.out.println(
    NumberList.toString(); 
    //); 

} 


/** Constructs an empty number list. */ 
public NumberList(){ 
    longArray = new long[0];  
    //System.out.println(longArray.length); 
} 


/** Constructs a number list from an array of Longs. */ 
public NumberList(Long[] l){ 
    int size = l.length; 
    longArray = new long[size]; 
    for (int i = 0; i < size; i++) { 
     longArray[i] = l[i]; 
    } 
} 

/** This returns a stringy version of this number list. */ 
public String toString() { 
    //System.out.println(this.length()); 
    return "why doesnt this work :("; 
} 

/** Increases by one the number of instances of the given element in this collection. */ 
public boolean add (Object obj) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    //return true if the element CAN be added 
    throw new UnsupportedOperationException(); 
} 


/** Adds all of the elements of the given number list to this one. */ 
public boolean addAll (java.util.Collection c ) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 


/** Removes all of the elements from this collection. */ 
public void clear() { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 


/** Returns true iff this number list contains at least one instance of the specified element. */ 
public boolean contains (Object obj) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    // 
    throw new UnsupportedOperationException(); 
} 



/** Returns true iff this number list contains at least one instance of each element 
    in the specified list. Multiple copies of some element in the argument do not 
    require multiple copies in this number list. */ 
public boolean containsAll (java.util.Collection c) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 




/** Compares the specified object with this collection for equality. */ 
public boolean equals (Object obj) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 




/** Returns the hashcode value for this collection. */ 
public int hashCode() { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    //return integer that represents the set uniquely 
    //return hashcode based on the numbers in the array 
    //hashCode should be equal in equal cases 
    throw new UnsupportedOperationException(); 
} 



/** Returns true if this collection contains no elements. */ 
public boolean isEmpty() { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 



/** Returns an iterator over the elements in this collection. Replicated elements should 
    be "iterated over" just once. */ 
public java.util.Iterator iterator() { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 



/** Removes a single instance of the specified element from 
    this collection, if it is present. */ 
public boolean remove (Object obj) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 



/** Removes all of this collection's elements that are also contained 
    in the specified collection. */ 
public boolean removeAll (java.util.Collection c) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 




/** Retains only the elements in this collection that are contained in the specified collection. 
    In other words, removes from this collection all of its elements that are not contained in the 
    specified collection. */ 
public boolean retainAll (java.util.Collection c) { 
    throw new UnsupportedOperationException(); 
} 


/** Returns the number of elements in this number list, including duplicates. */ 
public int sizeIncludingDuplicates() { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 



/** Returns a Long[] containing all of the elements in this collection, not including duplicates. */ 
public Long[] toArray() { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 



/** Not supported for this class. */ 
public Object[] toArray (Object[] obj) { 
    throw new UnsupportedOperationException(); 
} 




/** Returns the number of elements in this number list, not including duplicates. */ 
public int size() { 
    System.out.println(longArray.length); 
    return 0 ; 
} 




/** Returns the number of instances of the given element in this number list. */ 
public int count (Object obj) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 



/** This so-called "static factory" returns a new number list comprised of the numbers in the specified array. 
    Note that the given array is long[], not Long[]. */ 
public static NumberList fromArray (long[] l) { 
    /* REPLACE THE NEXT STATEMENT WITH YOUR CODE */ 
    throw new UnsupportedOperationException(); 
} 

}

+0

'NumberList.toString();' - 'toString'不是一个静态方法,你需要的类的实例来调用它,'新NumberList()。 toString()'例如 – MadProgrammer

+1

“为什么!?”你对“静态”有多少了解?您需要在'NumberList'的*特定实例*上调用'toString()',否则它没有任何意义。我建议你回到你的Java书籍/教程/任何地方,找到关于'static'成员和实例成员的部分。 –

+0

接下来,每个帖子有一个问题,请 - 你的构造函数问题(看起来像'Long []'不兼容'long []')是完全独立的。 –

回答

0

它不是一个静态的方法,在NumberList的toString只能呼吁对象NumberList的实例。

您必须使用它作为

//create a instance of nList 
NumberList nList = new NumberList(); 
//call the method on the newly created instance of NumberList 
nList.toString();