JavaRush /Blog Java /Random-FR /10 techniques de tableaux couramment utilisées en Java
theGrass
Niveau 24
Саратов

10 techniques de tableaux couramment utilisées en Java

Publié dans le groupe Random-FR
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. 10 techniques de tableaux couramment utilisées en Java - 1
  1. 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"};
  2. 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]
  3. 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]
  4. 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
  5. 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);
  6. Déclarer un tableau sur une ligne

    method(new String[]{"a", "b", "c", "d", "e"});
  7. 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
  8. Convertir ArrayListen tableau

    String[] 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);
  9. 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]
  10. 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]
  11. 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 bytebasé sur une valeur d'un type int(environ prendre ByteBuffer, sélectionnez-y 4 octets et mettez le nombre int8, puis convertissez tout cela (0, 0, 0, 8) en un tableau de type byteutilisant l'appel array())

    byte[] bytes = ByteBuffer.allocate(4).putInt(8).array();
    
    for (byte t : bytes) {
       System.out.format("0x%x ", t);
    }
Commentaires
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION