„Ето няколко примера за интересни неща, които можете да правите с масиви:“

Пример 1.
Попълнете масив от 10 елемента с числа от 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;
        }
    }
}
Попълнете масив от 10 елемента с числа от 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;
        }
    }
}
Попълнете масив от 10 елемента с числа от 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;
        }
    }
}
Попълнете масив от 10 елемента с числа от 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;
        }
    }
}
Пример 2.
Прочетете 10 низа от клавиатурата:
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();
     }
  }
}
Прочетете 10 числа от клавиатурата:
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);
    }
  }
}
Пример 3.
Показване на масив на екрана:
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]);
    }
}
Пример 4.
Бърза (статична) инициализация. Добавете елементите на масива:
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);
    }
}
Пример 5.
Намерете най-малкия елемент в масив:
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);
    }
}

Фрагмент от лекция с ментор като част от курса на Codegym University. Запишете се за пълния курс.