Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Map implementieren

    Java Code:
    public interface ElemT {
    
    
    	public ElemT copy();
    
    	
    	public boolean equals(ElemT e);
    
    	
    	public String toString();
    
    } // interface ElemT
    Java Code:
    public class ArrayListT implements ElemT, ListT {
    
    	
        private int size = 0;          
        private ElemT[] array;
        private static final int DEFAULT_SIZE = 100;
    	private static final int NIL = -1;
        private int maxs;
        
         public ArrayListT() {
        	this(DEFAULT_SIZE);
        }// ArrayListT
         
        public ArrayListT(int max) { 
    	if (max > 0) {
    		maxs = max;
    		array = new ElemT[maxs];
    		size=0;
    	
        } }// ArrayListT(int max)
        
       //implementierung ElemT
      
        public ElemT copy() {
        	ArrayListT l = new ArrayListT(maxs);
        	l.size = size;
        	for (int i = 0; i < size; i++) {
        	    l.array[i] = array[i].copy();
        	}
        	return l;
            } // copy
          
            public boolean equals(ElemT e) {
            	
            	if (e == null) {
        			return false;
        		}
            	if ( (e instanceof ArrayListT)) {
            		ArrayListT l = (ArrayListT) e;
            		
            	if (size != l.size) {
            	    return false;
            	}
            	for (int i = 0; i < size; i++) {
            	    if (!array[i].equals(l.array[i])) {
            		return false;
            	    }
            	}
            	return true;
            	}
            	return false;
            } // equals
    
            public boolean less(ElemT e) {
            	if (e == null) {
        			return false;
        		}
    
        		if (e instanceof ArrayListT) {
        			ArrayListT l = (ArrayListT) e;
    
        			if (size != l.size) {
        				return false;
        			}
            	
            	for (int i = 0; i < size; i++) {
            	    if (!array[i].less(l.array[i])) {
            		return false;
            	    }
            	}
            	return true;
            }
            return false;
                } // less
          
            public String toString() {
        	String s = "<";
        	for(int i = 0; i < size; i++) {
        	    if (i == 0) {
        		s += array[i].toString();
        	    } else {
        		s += ", " + array[i].toString();
        	    }
        	}
        	s += ">";
        	return s;
            } // toString
                
        
    } // class ArrayListT
    Java Code:
    public interface MapT {
    	
    	
    	void clear();
    	
    	
    	void assign(ElemT key, ElemT value);
    	
    	
    	ElemT compute(ElemT key);
    }
    Java Code:
    public class Mapping implements MapT{
    
    	
    	private int maxSIZE;
    	private ElemT[] mapping;
    
    	public Mappping(){
    		maxSIZE=100;
    		mapping = new ElemT[maxSIZE];
    		}
    
    	
    	public void clear() {
    		for(int i=0; i<maxSIZE;i++)
    		mapping[i] = null;
    	}
    
    	
    	public void assign(ElemT key, ElemT value) {
    		if(...>=0 || ... <maxSIZE) {//soll pruefen ob ElemT key größer als Null ist und kleiner als maxSIZE ist
    	 	mapping[... = value];}// von will ich definieren das meine funktion mapping von Key zu value definiert ist, also mapping(key)=value
    		else	
    	    System.exit(1);//muesste eigentlich nen fehler anzeigen
    	    	
    	}
    
    
    	
    	public ElemT compute(ElemT key) {
    		
    
    	}
    
    }
    Da ich mit meiner Methode assign nicht weiter komme, wollte ich fragen ob mir vielleicht jemand helfen kann, denn ich weiss nicht wie ich mapping(key)=value definieren soll.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    CA, USA
    Posts
    264
    Rep Power
    6

    Default Re: Map implementieren

    I don't speak German, so I apologize if I'm not understanding the question right. But it seems like you're trying to implement an associative array. If you just need one, look into HashMap. If it's something like an assignment where you have to actually implement it yourself, that will be more complex.

    Java Code:
    private Map<ElemT, ElemT> map = new HashMap<ElemT, ElemT>();
    ...
    public void assign(ElemT key, ElemT value)
    {
    if(key.intVal() >= 0 && key.inVal() < maxSIZE) //implement intVal() to return an int, or use hashCode()
      map.put(key, value);
    else
      //handle error
    }
    I'm not sure this answers your question, but hopefully it will help in some way.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •