我无法弄清楚如何让事件发生,它只输出0。 输出应如下所示:
列表:2 1 0 1 0 1 4 3 4 2 4 3 3 0 0 4 0 1 3 2 出现0:5 发生1:4 发生2:3 发生在3:4 发生4:4
public class PEX6
{
private static int CountValue
(ListInterface<Integer> theList, int theValue)
{
theList.reset();
int nFound = 0;
for (int i=0; i<=19; i++)
{
theList.getNext();
if (theList.equals(theValue))
{
nFound++;
}
}
return nFound;
}
public static void main(String[] args)
{
ListInterface<Integer> theList = new RefList<Integer>();
for (int i = 0; i <= 19; i++)
{
int Random = ((int)(Math.random()*4));
theList.add(new Integer(Random));
}
theList.writeLinkedList();
theList.toString();
int [] valueCount = new int[5];
for (int i = 0; i < 4; i++)
{
valueCount[i] = CountValue(theList, i);
}
System.out.println("Occurrences of 0:"+ valueCount[0]);
System.out.println("Occurrences of 1:"+ valueCount[1]);
System.out.println("Occurrences of 2:"+ valueCount[2]);
System.out.println("Occurrences of 3:"+ valueCount[3]);
System.out.println("Occurrences of 4:"+ valueCount[4]);
}
}
主文件实现这两个文件:
class LLObjectNode<T>
{
private LLObjectNode<T> link;
private T info;
public LLObjectNode ( T theInfo )
{
info = theInfo;
link = null;
}
public void setInfo ( T theInfo )
{
info = theInfo;
}
public T getInfo()
{
return info;
}
public void setLink ( LLObjectNode<T> theLink )
{
link = theLink;
}
public LLObjectNode<T> getLink()
{
return link;
}
}
// Stores and manipulates collection of objects in accordance with ListInterface functionality:
public class RefList<T> implements ListInterface<T>
{
// Stores number of elements within list:
protected int numElements;
/*
* Points to first node of list:
*
* This variable will be set to null, when list is empty.
*/
protected LLObjectNode<T> first;
/*
* Used for iteration through list:
*
* This variable will be set to null until call is made to reset() method,
* at which this variable will point to first element of list:
*/
protected LLObjectNode<T> currentPos;
// Used to locate elements on list:
// Set to true if find (...) is successful:
protected boolean found;
// Points to the found element:
protected LLObjectNode<T> location;
// Points to element preceding the found element:
protected LLObjectNode<T> previous;
/*
* Attempts to find element contained within list that exists as a copy
* of the given object:
*
* If find is successful, the found variable will be set to true, the location
* variable will point to found element, and the previous variable will point to the
* element preceding the found element, unless the found element is the first element of list.
*/
protected void find ( T theTarget )
{
found = false;
location = first;
previous = null;
while ( location != null )
{
if ( location.getInfo().equals( theTarget ))
{
found = true;
break;
}
else
{
previous = location;
location = location.getLink();
}
}
}
// Creates empty list:
public RefList()
{
currentPos = null;
first = null;
numElements = 0;
}
// Returns number of elements within list:
public int size()
{
return numElements;
}
// Returns true if this list contains copy of given object:
public boolean contains ( T theTarget )
{
find( theTarget );
return found;
}
// Removes first element found in list that exists as copy of given
// object and returns true if such element was found:
public boolean remove ( T theTarget )
{
find( theTarget );
if ( found )
{
if ( location == first )
first = first.getLink();
else
previous.setLink( location.getLink());
--numElements;
}
return found;
}
// Returns reference to first element found within list that exists as copy
// of given object or null if no such element was found:
public T get ( T theTarget )
{
find( theTarget );
if ( found )
return location.getInfo();
else
return null;
}
// Returns a nicely formatted string representation fo this list:
public String toString()
{
LLObjectNode node = first;
StringBuffer buff = new StringBuffer ( "List:" );
while ( node != null )
{
buff.append( " " + node.getInfo());
node = node.getLink();
}
return buff.toString();
}
// Prints contents of this list to screen:
public void writeLinkedList()
{
System.out.println( toString());
}
// Initializes this list for iteration:
public void reset()
{
currentPos = first;
}
/*
* Returns reference to element located at currentPos and increments currentPos
* to point to next element contained within list:
*
* If correntPos is pointing to last element in list, it will be reset to point to
* first element in list.
*
* @Preconditions:
* This list is not empty.
* This list has been reset.
* This list has not been modified since last reset.
*/
public T getNext()
{
T next = currentPos.getInfo();
if ( currentPos.getLink()== null )
currentPos = first;
else
currentPos = currentPos.getLink();
return next;
}
// Inserts given object onto front of list:
public void add ( T theObject )
{
LLObjectNode<T> node = new LLObjectNode<T> ( theObject);
node.setLink( first );
first = node;
++numElements;
}
}
public interface ListInterface<T>
{
// Returns number of elements within list:
int size();
/* Returns tru if list contains a copy of given object:
*
* Comparisons should be performed by calling the equals(...) method of
* each element, passing the given object as an argument
*/
boolean contains ( T theObject);
/*
* Removes the first element found within this list that exists as a copy of
* the given object and returns true if element was found:
*/
boolean remove ( T theObject);
/*
* Returns reference to first element found within this list that exists as a
* copy of the given object or null if no such element was found:
*/
Object get ( T element);
// Returns a nicely formatted string representation of this list:
String toString();
// Prints the contents of this list to screen:
void writeLinkedList();
// Initializes this list for iteration (use of the getNext() method):
void reset();
/*
* Returns reference to the element located at the iterator's current
* position and increment the iterator:
*
* If iterator is currently pointing to the last element in this list,
* the iterator should be reset to point to first element in list.
*
* @Preconditions:
* This list is not empty.
* This list has been reset.
* This list has not been modified since last reset.
*/
T getNext();
// Inserts the given object onto front of list:
void add ( T theObject);
}
答案 0 :(得分:1)
您要求列表中的下一个值,但忽略它:
theList.getNext();
然后您将列表本身与值进行比较:
if (theList.equals(theValue))
尝试这样的事情:
if (theList.getNext().equals(theValue))
// or
if (theList.getNext() == theValue)
答案 1 :(得分:1)
这不起作用:
theList.getNext();
if (theList.equals(theValue))
{
nFound++;
}
您不会将当前列表元素与theValue进行比较,而是将List本身永远不为真,因此nFound仍为0。
应该是:
element = theList.getNext();
if (element.equals(theValue))
{
nFound++;
}