# Generische Listen und Iteration



## Wunderwuzi (21. Jan 2012)

Hallo!

Ich soll folgendes Programm bei den Stellen die mit //TODO markiert sind ergänzen.
Aufgabenstellung in den Kommentaren.
Hab auch schon angefangen, allerdings bekomm ich Fehlermeldungen.
Kann mir vielleicht jemand eine kurze Erklärung geben, wie das funktioniert?

Danke!


```
package ab6;

import java.util.Iterator;

/** FrequencyListIterative.java
 * A frequency list is a list of items along with their frequencies.
 * For this implementation, the generic type parameter T represents the
 * type of item whose frequencies we are counting, and we define the
 * frequency of item T to be the number of times add(T) has been called.
 * 
 * The underlying data structure for the implementation is a GenList 
 * of FrequencyPair<T> objects, each of which stores an item
 * of type T and an integer frequency.
 */
public class FrequencyListIterative<T> {

	/** The list of items and frequencies, stored in any order. */
	private GenList<FrequencyPair<T>> flist;

	/** Creates a new frequency list, initially empty. */
	public FrequencyListIterative() {
		//TODO
		this.flist= new GenList<FrequencyPair<T>>();
	}

	/** Checks if this frequency list is empty.
	 * @return: true iff this frequency list is empty.
	 */
	public boolean empty() {
		// TODO
		return flist.first==null;
	}

	/** Gets the length of this frequency list.
	 * @return: The number of distinct items that have been
	 *         added to this frequency list.
	 */
	public int length() {
		//TODO
		int size=0;
		for(Iterator i=flist.iterator();i.hasNext();){
			size++;
			i.next();
		}
		return size;
				
	}

	/** Adds an item to this frequency list.
	 * pre: item != null
	 * post: If item has not been added before, it is
	 *       added to the frequency list with frequency 1.
	 *       Otherwise, the frequency of item in the list
	 *       is increased by 1.
	 */
	public void add (T item) {
		//TODO
	
		flist.add(length()-1, item);
		
	}

	/** Gets the most frequent item in the list.
	 * @return: The most frequently added item.
	 *         If there is more than one item with the highest
	 *         frequency, any one of them is returned.
	 */
	public T mostFrequent() {
		//TODO
		
		return null;
	}

	/** Gets the k most frequently added items, along with their frequencies.
	 * @return: A GenList of FrequencyPair<T>s containing the k items with
	 *         the highest frequencies, sorted from least to most frequent.
	 *         If this.length() < k, then the length of the returned list
	 *         is this.length(); otherwise it is k.
	 */
	public GenList<FrequencyPair<T>> mostFrequent( int k ) {
		//TODO
		return null;
	}

	/** Helper method for mostFrequent(int k) which inserts a given item into
	 * the given list so that the result is sorted by increasing frequency.
	 * @return: A GenList of FrequencyPair<T>s with all the items in lst,
	 *         plus item, sorted from least to most frequent.
	 */
	private GenList<FrequencyPair<T>> insert( FrequencyPair<T> item, GenList<FrequencyPair<T>> lst ) {
		if( null == lst )
			return new GenList<FrequencyPair<T>>(item,null);
		else if( item.lessFrequentThan(lst.first) )
			return new GenList<FrequencyPair<T>>(item,lst);
		else
			return new GenList<FrequencyPair<T>>(lst.first,insert(item,lst.rest));
	}
}
```


----------



## qowaz (21. Jan 2012)

Kannst du die Fehlermeldungen mal posten?


----------



## Wunderwuzi (21. Jan 2012)

Zeile 23
The constructor GenList<FrequencyPair<T>>() is undefined


Zeile 41
Multiple markers at this line
	- Iterator is a raw type. References to generic type Iterator<E> should be 
	 parameterized
	- The method iterator() is undefined for the type GenList<FrequencyPair<T>>       

Zeile 59
The method add(int, T) is undefined for the type GenList<FrequencyPair<T>>


----------



## Gast2 (21. Jan 2012)

> The constructor GenList<FrequencyPair<T>>() is undefined


Da liegt der Fehler im Konstruktor der Klasse GenList, hat die nen leeren Konstruktor?



> The method add(int, T) is undefined for the type GenList<FrequencyPair<T>>


Du hast flist als Container für FrequencyPair<T> Objekte definiert. Du versuchst aber ein T reinzulegen, das geht nicht.



> - Iterator is a raw type. References to generic type Iterator<E> should be
> parameterized
> - The method iterator() is undefined for the type GenList<FrequencyPair<T>>


Wenn du nen Iterator verwendest, dann gib immer den Typ mit an. In deinem Fall wäre das wohl 
	
	
	
	





```
Iterator<FrequencyPair<T>>
```
. Allerdings implementiert deine GenList wohl nicht iterable


----------

