-
Déclaration de tableau
String[] aArray = new String[5]; String[] bArray = {"a","b","c", "d", "e"}; String[] cArray = new String[]{"a","b","c","d","e"};
-
Sortie de tableau en Java
int[] intArray = { 1, 2, 3, 4, 5 }; String intArrayString = Arrays.toString(intArray); // print directly will print reference value System.out.println(intArray); // [I@7150bd4d System.out.println(intArrayString); // [1, 2, 3, 4, 5]
-
Création d'une ArrayList à partir d'un tableau
String[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray)); System.out.println(arrayList); // [a, b, c, d, e]
-
Vérifier un tableau pour une valeur spécifique
String[] stringArray = { "a", "b", "c", "d", "e" }; boolean b = Arrays.asList(stringArray).contains("a"); System.out.println(b); // true
-
Fusionner deux tableaux
int[] intArray = { 1, 2, 3, 4, 5 }; int[] intArray2 = { 6, 7, 8, 9, 10 }; // Apache Commons Lang library int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
-
Déclarer un tableau sur une ligne
method(new String[]{"a", "b", "c", "d", "e"});
-
Concaténation d'éléments de tableau dans une chaîne
// containing the provided list of elements // Apache common lang String j = StringUtils.join(new String[] { "a", "b", "c" }, ", "); System.out.println(j); // a, b, c
-
Convertir
ArrayList
en tableauString[] stringArray = { "a", "b", "c", "d", "e" }; ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray)); String[] stringArr = new String[arrayList.size()]; arrayList.toArray(stringArr); for (String s : stringArr) System.out.println(s);
-
Conversion d'un tableau en un ensemble (
Set
)Set<String> set = new HashSet<String>(Arrays.asList(stringArray)); System.out.println(set); //[d, e, b, c, a]
-
Renvoie un tableau avec des éléments dans l'ordre inverse
int[] intArray = { 1, 2, 3, 4, 5 }; ArrayUtils.reverse(intArray); System.out.println(Arrays.toString(intArray)); //[5, 4, 3, 2, 1]
-
Supprimer un élément d'un tableau
int[] intArray = { 1, 2, 3, 4, 5 }; int[] removed = ArrayUtils.removeElement(intArray, 3);//create a new array System.out.println(Arrays.toString(removed));
Et encore une chose - créer un tableau d'un type
byte
basé sur une valeur d'un typeint
(environ prendreByteBuffer
, sélectionnez-y 4 octets et mettez le nombreint
8, puis convertissez tout cela (0, 0, 0, 8) en un tableau de typebyte
utilisant l'appelarray()
)byte[] bytes = ByteBuffer.allocate(4).putInt(8).array(); for (byte t : bytes) { System.out.format("0x%x ", t); }
10 techniques de tableaux couramment utilisées en Java
Les 10 techniques suivantes pour les tableaux en Java sont couramment utilisées. Ils ont le classement le plus élevé sur Stack Overflow, un système de questions et réponses sur la programmation.
GO TO FULL VERSION