我知道这对于像我这样的新手是一个相当常见的错误,但我似乎无法阅读任何我能理解的内容。对于我的作业,我们需要创建一个名为NumberList的long数组。我们需要完成的方法之一是toString,它显然将数组转换为要打印的字符串。
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 :(得分:0)
这不是静态方法,只能在NumberList对象的实例上调用NumberList上的toString。
您必须将其用作
//create a instance of nList
NumberList nList = new NumberList();
//call the method on the newly created instance of NumberList
nList.toString();