CodeGym /Cours /Syntaxe Java /Actions de base avec les tableaux

Actions de base avec les tableaux

Syntaxe Java
Niveau 7 , Leçon 3
Disponible

« Voici quelques exemples de choses intéressantes que tu peux faire avec les tableaux : »

Exemple 1.
Remplit un tableau de 10 éléments avec des nombres de 1 à 10 :
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = i + 1;
        }
    }
}
Remplit un tableau de 10 éléments avec des nombres de 10 à 1 :
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = 10 - i;
        }
    }
}
Remplit un tableau de 10 éléments avec des nombres de 0 à 9 :
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = i;
        }
    }
}
Remplit un tableau de 10 éléments avec des nombres de 9 à 0 :
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = 9 - i;
        }
    }
}
Exemple 2.
Lit 10 chaînes saisies au clavier :
public class MainClass
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String[] list = new String[10];

    for (int i = 0; i < list.length; i++)
    {
      list[i] = reader.readLine();
     }
  }
}
Lit 10 nombres saisis au clavier :
public class MainClass
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] list = new int[10];

    for (int i = 0; i < list.length; i++)
    {
      String s = reader.readLine();
      list[i] = Integer.parseInt(s);
    }
  }
}
Exemple 3.
Affiche un tableau à l'écran :
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        int[] list = new int[10];

        // Fill the array
        for (int i = 0; i < list.length; i++) 
           list[i] = i;

        // Display the contents of the array
        for (int i = 0; i < list.length; i++)  
          System.out.println(list[i]);
    }
}
Exemple 4.
Initialisation rapide (statique). Ajoute des éléments de tableau :
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        // Static initialization
        int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};        
        
        // Calculate the sum
        int sum = 0;
        for (int i = 0; i < list.length; i++) 
           sum += list[i];

        System.out.println("Sum is " + sum);
    }
}
Exemple 5.
Trouve le plus petit élément dans un tableau :
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};

        int min = list[0];
        for (int i = 1; i < list.length; i++)
        {
             if (list[i] < min) 
                  min = list[i];
        }

       System.out.println ("Min is " + min);
    }
}
Commentaires (2)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Christophe niveau 22, Lausanne, Switzerland
12 avril 2020
to find the smallest or greatest element in array, a laziest way is to use

Arrays.sort(myListOfNumber)
to sort your array from min to max and then just access the first

myListOfNumber[0]
or the last

myListOfNumber[myListOfNumber.lenght-1]
;-)
Anonymous #11581840 niveau 15, Issy-les-Moulineaux, France
20 novembre 2024
But it doesn't help understanding using arrays and loops... which is the point here