„Iată câteva exemple de lucruri interesante pe care le puteți face cu matrice:”

Exemplul 1.
Umpleți o matrice de 10 elemente cu numere de la 1 la 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;
        }
    }
}
Umpleți o matrice de 10 elemente cu numere de la 10 la 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;
        }
    }
}
Umpleți o matrice de 10 elemente cu numere de la 0 la 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;
        }
    }
}
Umpleți o matrice de 10 elemente cu numere de la 9 la 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;
        }
    }
}
Exemplul 2.
Citiți 10 șiruri de caractere de la tastatură:
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();
     }
  }
}
Citiți 10 numere de la tastatură:
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);
    }
  }
}
Exemplul 3.
Afișați o matrice pe ecran:
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]);
    }
}
Exemplul 4.
Inițializare rapidă (statică). Adunați elementele matricei:
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);
    }
}
Exemplul 5.
Găsiți cel mai mic element dintr-o matrice:
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);
    }
}

Un fragment de prelegere cu un mentor ca parte a cursului Universității Codegym. Înscrie-te la cursul complet.