-3
我知道這是像我這樣的noobs一個相當常見的錯誤,但我似乎無法讀懂任何我能理解的東西。對於我的作業,我們需要創建一個名爲NumberList的long數組。我們需要完成的一個方法叫做toString,它明顯地將數組轉換爲一個字符串來打印。Java錯誤:靜態方法無法引用非靜態方法
- 因此,在我的主要方法中,我簡單地創建了一個空白的NumberList對象,並嘗試在其上運行toString()方法。編譯器說「不能從靜態上下文中引用非靜態方法toString()」。爲什麼!?
- 其次,我創建了一個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();
}
}
'NumberList.toString();' - 'toString'不是一個靜態方法,你需要的類的實例來調用它,'新NumberList()。 toString()'例如 – MadProgrammer
「爲什麼!?」你對「靜態」有多少了解?您需要在'NumberList'的*特定實例*上調用'toString()',否則它沒有任何意義。我建議你回到你的Java書籍/教程/任何地方,找到關於'static'成員和實例成員的部分。 –
接下來,每個帖子有一個問題,請 - 你的構造函數問題(看起來像'Long []'不兼容'long []')是完全獨立的。 –