Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    957

    [C# noob]Hashtable da Java

    Ciao a tutti,
    sono un novellino di C#, ho scritto oggi il mio primo programma

    Volevo convertire un programma Java in C# e mi serve quello che in Java era una classe HashMap.
    Mi par di aver trovato la classe Hashtable sotto "Collections", che però ha Add (al posto di put) ma non ha get.
    Cosa devo usare per ottenere l'elemento nella mappa associato alla relativa key?

    Grazie

  2. #2
    Utente di HTML.it L'avatar di albgen
    Registrato dal
    Jun 2005
    Messaggi
    3,249
    bisogna utilizzare le []
    esempio:

    Hashtable ht = new Hashtable();
    ht.Add("chiave1", "valore1");
    ht.Add("chiave2", "valore2");
    Console.WriteLine(ht["chiave1"].ToString());
    I got the remedy

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    957
    che lol...

    Ma pondendo che devo estendere una classe, diciamo da lista concatenata a lista concatenata ordinata:
    codice:
    	private class PriorityList: LinkedList<Node>{
    			
    		
    		public bool add(Node obj) 
    		{
    			for (int i = 0; i < Count(); i++) 
    			{
    				if (obj.compareTo(this[i]) <= 0) 
    				{
                        AddBefore(this[i], obj);
    					return true;
    				}
    			}
                
    			AddLast(obj);
    			return true;
    		}
    	}
    quel this[i] non piace, come faccio a riferirmi alla iesimo elemento della linkedlist corrente che ho esteso a prioritylist?

    Altra piccola cosa, in java in questa classe avevo il metodo indexOf() che da oggetto mi dava la posizione, qui cosa c'è?

    Grazie mille

  4. #4
    Ecco cosa ha la Hashtable in C#:

    Codice PHP:
            // Summary:
            //     Adds an element with the specified key and value into the System.Collections.Hashtable.
            //
            // Parameters:
            //   value:
            //     The value of the element to add. The value can be null.
            //
            //   key:
            //     The key of the element to add.
            //
            // Exceptions:
            //   System.NotSupportedException:
            //     The System.Collections.Hashtable is read-only.-or- The System.Collections.Hashtable
            //     has a fixed size.
            //
            //   System.ArgumentException:
            //     An element with the same key already exists in the System.Collections.Hashtable.
            //
            //   System.ArgumentNullException:
            //     key is null.
            
    public virtual void Add(object keyobject value);
            
    //
            // Summary:
            //     Removes all elements from the System.Collections.Hashtable.
            //
            // Exceptions:
            //   System.NotSupportedException:
            //     The System.Collections.Hashtable is read-only.
            
    [ReliabilityContract(32)]
            public 
    virtual void Clear();
            
    //
            // Summary:
            //     Creates a shallow copy of the System.Collections.Hashtable.
            //
            // Returns:
            //     A shallow copy of the System.Collections.Hashtable.
            
    public virtual object Clone();
            
    //
            // Summary:
            //     Determines whether the System.Collections.Hashtable contains a specific key.
            //
            // Parameters:
            //   key:
            //     The key to locate in the System.Collections.Hashtable.
            //
            // Returns:
            //     true if the System.Collections.Hashtable contains an element with the specified
            //     key; otherwise, false.
            //
            // Exceptions:
            //   System.ArgumentNullException:
            //     key is null.
            
    public virtual bool Contains(object key);
            
    //
            // Summary:
            //     Determines whether the System.Collections.Hashtable contains a specific key.
            //
            // Parameters:
            //   key:
            //     The key to locate in the System.Collections.Hashtable.
            //
            // Returns:
            //     true if the System.Collections.Hashtable contains an element with the specified
            //     key; otherwise, false.
            //
            // Exceptions:
            //   System.ArgumentNullException:
            //     key is null.
            
    public virtual bool ContainsKey(object key);
            
    //
            // Summary:
            //     Determines whether the System.Collections.Hashtable contains a specific value.
            //
            // Parameters:
            //   value:
            //     The value to locate in the System.Collections.Hashtable. The value can be
            //     null.
            //
            // Returns:
            //     true if the System.Collections.Hashtable contains an element with the specified
            //     value; otherwise, false.
            
    public virtual bool ContainsValue(object value);
            
    //
            // Summary:
            //     Copies the System.Collections.Hashtable elements to a one-dimensional System.Array
            //     instance at the specified index.
            //
            // Parameters:
            //   array:
            //     The one-dimensional System.Array that is the destination of the System.Collections.DictionaryEntry
            //     objects copied from System.Collections.Hashtable. The System.Array must have
            //     zero-based indexing.
            //
            //   arrayIndex:
            //     The zero-based index in array at which copying begins.
            //
            // Exceptions:
            //   System.ArgumentNullException:
            //     array is null.
            //
            //   System.InvalidCastException:
            //     The type of the source System.Collections.Hashtable cannot be cast automatically
            //     to the type of the destination array.
            //
            //   System.ArgumentOutOfRangeException:
            //     arrayIndex is less than zero.
            //
            //   System.ArgumentException:
            //     array is multidimensional.-or- arrayIndex is equal to or greater than the
            //     length of array.-or- The number of elements in the source System.Collections.Hashtable
            //     is greater than the available space from arrayIndex to the end of the destination
            //     array.
            
    public virtual void CopyTo(Array array, int arrayIndex);
            
    //
            // Summary:
            //     Returns an System.Collections.IDictionaryEnumerator that iterates through
            //     the System.Collections.Hashtable.
            //
            // Returns:
            //     An System.Collections.IDictionaryEnumerator for the System.Collections.Hashtable.
            
    public virtual IDictionaryEnumerator GetEnumerator();
            
    //
            // Summary:
            //     Returns the hash code for the specified key.
            //
            // Parameters:
            //   key:
            //     The System.Object for which a hash code is to be returned.
            //
            // Returns:
            //     The hash code for key.
            //
            // Exceptions:
            //   System.NullReferenceException:
            //     key is null.
            
    protected virtual int GetHash(object key);
            
    //
            // Summary:
            //     Implements the System.Runtime.Serialization.ISerializable interface and returns
            //     the data needed to serialize the System.Collections.Hashtable.
            //
            // Parameters:
            //   context:
            //     A System.Runtime.Serialization.StreamingContext object containing the source
            //     and destination of the serialized stream associated with the System.Collections.Hashtable.
            //
            //   info:
            //     A System.Runtime.Serialization.SerializationInfo object containing the information
            //     required to serialize the System.Collections.Hashtable.
            //
            // Exceptions:
            //   System.ArgumentNullException:
            //     info is null.
            
    public virtual void GetObjectData(SerializationInfo infoStreamingContext context);
            
    //
            // Summary:
            //     Compares a specific System.Object with a specific key in the System.Collections.Hashtable.
            //
            // Parameters:
            //   item:
            //     The System.Object to compare with key.
            //
            //   key:
            //     The key in the System.Collections.Hashtable to compare with item.
            //
            // Returns:
            //     true if item and key are equal; otherwise, false.
            //
            // Exceptions:
            //   System.ArgumentNullException:
            //     item is null.-or- key is null.
            
    protected virtual bool KeyEquals(object itemobject key);
            
    //
            // Summary:
            //     Implements the System.Runtime.Serialization.ISerializable interface and raises
            //     the deserialization event when the deserialization is complete.
            //
            // Parameters:
            //   sender:
            //     The source of the deserialization event.
            //
            // Exceptions:
            //   System.Runtime.Serialization.SerializationException:
            //     The System.Runtime.Serialization.SerializationInfo object associated with
            //     the current System.Collections.Hashtable is invalid.
            
    public virtual void OnDeserialization(object sender);
            
    //
            // Summary:
            //     Removes the element with the specified key from the System.Collections.Hashtable.
            //
            // Parameters:
            //   key:
            //     The key of the element to remove.
            //
            // Exceptions:
            //   System.NotSupportedException:
            //     The System.Collections.Hashtable is read-only.-or- The System.Collections.Hashtable
            //     has a fixed size.
            //
            //   System.ArgumentNullException:
            //     key is null.
            
    [ReliabilityContract(31)]
            public 
    virtual void Remove(object key);
            
    //
            // Summary:
            //     Returns a synchronized (thread safe) wrapper for the System.Collections.Hashtable.
            //
            // Parameters:
            //   table:
            //     The System.Collections.Hashtable to synchronize.
            //
            // Returns:
            //     A synchronized (thread safe) wrapper for the System.Collections.Hashtable.
            //
            // Exceptions:
            //   System.ArgumentNullException:
            //     table is null.
            
    public static Hashtable Synchronized(Hashtable table); 

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.