aja..stimmt!
->blöder Fehler!
Danke für die schnelle Hilfe Beni! ;-)
ciao
->blöder Fehler!
Danke für die schnelle Hilfe Beni! ;-)
ciao
import java.util.*;
public class Map{
LinkedList list=new LinkedList();
KeyValuePair pair;
public void add(Object key,Object value){
if(list.contains(key)){
pair.setValue(value);
}
else{
list.add(new KeyValuePair(key,value));
}
}
public boolean contains(Object key){
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair) it.next();
if(pair.getKey().equals(key)){
return true;
}
}
return false;
}
public String toString(){
StringBuffer sb=new StringBuffer();
KeyValuePair pair;
String s;
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair) it.next();
s=""+pair.getKey()+""+pair.getValue()+" ";
sb.append(s);
}
s=sb.toString();
return s;
}
}
import java.util.*;
public class Map{
LinkedList list=new LinkedList();
public void add(Object key,Object value){
KeyValuePair pair=(KeyValuePair)contains(key);
if(pair!=null){
pair.setValue(value);
}
else{
list.add(new KeyValuePair(key,value));
}
}
public void delete(Object key){
KeyValuePair pair;
pair=(KeyValuePair)findKeyValue(key);
list.remove(pair);
}
public int giveSize(){
int cnt=0;
Iterator it=list.iterator();
while(it.hasNext()){
it.next();
cnt++;
}
return cnt;
}
public boolean isEmpty(){
if(list.size()==0) {
return true;
}
return false;
}
public Object findKeyValue(Object key){
KeyValuePair pair;
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair)it.next();
if(pair.getKey()==key){
return pair;
}
}
return null;
}
public Object contains(Object key){
KeyValuePair pair;
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair) it.next();
if(pair.getKey().equals(key)){
return pair;
}
}
return null;
}
public Object findValue(Object key){
KeyValuePair pair;
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair) it.next();
if(pair.getKey().equals(key)){
return pair.getValue();
}
}
return null;
}
public boolean isValueDefined(Object key){
KeyValuePair pair;
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair) it.next();
if(pair.getKey().equals(key)){
if(pair.getValue()!=null){
return true;
}
}
}
return false;
}
public Object [] getAllKeys(){
Object [] allKeys=new Object[list.size()];
Iterator it=list.iterator();
KeyValuePair pair;
int cnt=0;
while(it.hasNext()){
pair=(KeyValuePair) it.next();
allKeys[cnt]=pair.getKey();
cnt++;
}
return allKeys;
}
public Object [] getAllValues(){
Object [] allValues=new Object[list.size()];
KeyValuePair pair;
int cnt=0;
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair)it.next();
allValues[cnt]=pair.getValue();
cnt++;
}
return allValues;
}
public boolean equals(Map m){
//das fehlt noch ->ein Vergleich zweier Maps auf Gleichheit!
}
public String toString(){
StringBuffer sb=new StringBuffer();
KeyValuePair pair;
String s;
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair) it.next();
s=""+pair.getKey()+""+pair.getValue()+" ";
sb.append(s);
}
s=sb.toString();
return s;
}
}
public int giveSize(){
int cnt=0;
Iterator it=list.iterator();
while(it.hasNext()){
it.next();
cnt++;
}
return cnt;
}
public Object findKeyValue(Object key){
KeyValuePair pair;
Iterator it=list.iterator();
while(it.hasNext()){
pair=(KeyValuePair)it.next();
if(pair.getKey()==key){
return pair;
}
}
return null;
}
Ist nicht sonderlich kompliziert:Sindbad1983 hat gesagt.:Hast du eine Idee, wie ich an die Methode equals(Map p) herangehen könnte?
1) It is reflexive: for any non-null reference x, x.equals(x) should return true.
2) It is symmetric: for any references x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
3) It is transitive: for any references x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
4) It is consistent: If the objects to which x and y refer haven't changed, then repeated calls to x.equals(y) return the same value.
5) For any non-null reference x, x.equals(null) should return false.
public boolean equals(Object otherObject)
{
...
}