Premiers pas avec les tableaux

Tableau de base en Java

En Java, tout objet ou type primitif peut être un tableau. Les indices de tableau sont accessibles via arrayName[index], par ex. monTableau[0]. Les valeurs d’un tableau sont définies via myArray[0] = value, par ex. si myArray est un tableau de type String[] myArray[0] = "test";

public class CreateBasicArray{
    public static void main(String[] args){

        // Creates a new array of Strings, with a length of 1
        String[] myStringArray = new String[1]; 
        // Sets the value at the first index of myStringArray to "Hello World!"
        myStringArray[0] = "Hello World!";
        // Prints out the value at the first index of myStringArray,
        // in this case "Hello World!"
        System.out.println(myStringArray[0]);
            
        // Creates a new array of ints, with a length of 1
        int[] myIntArray = new int[1];
        // Sets the value at the first index of myIntArray to 1
        myIntArray[0] = 1;       
        // Prints out the value at the first index of myIntArray,
        // in this case 1
        System.out.println(myIntArray[0]);     
        
        // Creates a new array of Objects with a length of 1
        Object[] myObjectArray = new Object[1];
        // Constructs a new Java Object, and sets the value at the first     
        // index of myObjectArray to the new Object.
        myObjectArray[0] = new Object();
    }       
}

Disponibilité

Les tableaux sont disponibles dans la plupart des langages de programmation, utilisant souvent des crochets carrés [] ou ronds () pour accéder aux éléments, par ex. Carray[6] ou VBarray(6).