Come convertire ArrayList
in string[]
in C#?Come convertire ArrayList in string array (string []) in C#
risposta
string[] myArray = (string[])myarrayList.ToArray(typeof(string));
using System.Linq;
public static string[] Convert(this ArrayList items)
{
return items == null
? null
: items.Cast<object>()
.Select(x => x == null ? null : x.ToString())
.ToArray();
}
ho provato this.but sto ottenendo seguente errore Errore \t 'System.Collections.ArrayList' non contiene una definizione per 'Seleziona' e nessun metodo di estensione 'Seleziona' accettare un primo argomento di tipo 'sistema. Collections.ArrayList 'potrebbe essere trovato (ti manca una direttiva using o un riferimento di assembly?) –
Devi includere 'using System.Linq;' nella parte superiore del file. Inoltre mi mancava una chiamata '.Cast
Qualcuno si preoccupa di spiegare il downvote? la mia risposta non mi sembra affatto male ... – Nuffin
uso .ToArray(Type)
string[] stringArray = (string[])arrayList.ToArray(typeof(string));
emm ... ho scritto qualcosa di sbagliato, per ottenere downvote? :/ – Reniuz
Prova farlo con ToArray()
metodo.
ArrayList a= new ArrayList(); //your ArrayList object
var array=(String[])a.ToArray(typeof(string)); // your array!!!
È possibile utilizzare il metodo CopyTo dell'oggetto ArrayList.
Diciamo che abbiamo un arraylist, che ha String Type come Elements.
strArrayList.CopyTo(strArray)
Un semplice Google o ricerca su MSDN l'avrebbe fatto. Qui:
ArrayList myAL = new ArrayList();
// Add stuff to the ArrayList.
String[] myArr = (String[]) myAL.ToArray(typeof(string));
Un altro modo è il seguente.
System.Collections.ArrayList al = new System.Collections.ArrayList();
al.Add("1");
al.Add("2");
al.Add("3");
string[] asArr = new string[al.Count];
al.CopyTo(asArr);
- 1. Come convertire string [] in ArrayList?
- 2. Come convertire RDD [(String, String)] in RDD [Array [String]]?
- 3. Modifica String-Array in Strings.xml su ArrayList
- 4. Come posso convertire String [] per ArrayList <String>
- 5. può convertire string in array?
- 6. JSON String array in Java String lista
- 7. Come convertire NSSet in array [String]?
- 8. C# Array - string [] [] vs string [,]
- 9. Split String in String array
- 10. ArrayList <HashMap <String, String >> per String []
- 11. Come convertire Array in ArrayList in VB.Net
- 12. Converti Short Array in String C#
- 13. Come convertire String Array a doppio array in una linea
- 14. Come convertire String in Hex e Hex in String?
- 15. Come convertire String in JsonObject
- 16. Come convertire Guid [] in String?
- 17. Come convertire Writer in String
- 18. Come convertire Dictionary <string, object> dizionario <string, string> in C#
- 19. Verificare se String in String [] è in ArrayList <string>
- 20. Come iterare Arraylist <HashMap <String, String >>?
- 21. Java: Come convertire String [] in List o Set
- 22. Come convertire DataInputStream in String in Java?
- 23. Come convertire il char unsigned * in std :: string in C++?
- 24. convertire std :: string in basic_ostream?
- 25. Come convertire Bitmap in Base64 String
- 26. ArrayList e String [] AND Object []
- 27. convertire java.util.Map [String, Object] in scala.collection.immutable.Map [String, Any]
- 28. LINQ "cerniera" in String Array
- 29. Come convertire var a string []
- 30. Come trasformare System :: String^in std :: string?
ho provato questo.I sto ricevendo seguente errore su questo "Almeno un elemento nella matrice di origine non può essere lanciato verso il basso per il tipo di matrice di destinazione" –
So che questo è molto tardi, ma il motivo si sta ricevendo quell'errore è perché probabilmente hai una ArrayList con elementi che non sono stringhe, e stai provando a lanciare gli elementi sulla stringa, il che non ha alcun senso – Eames