JavaRush/Courses/New Java Syntax/Basic actions with arrays

Basic actions with arrays

Open

"Here are a few examples of interesting things you can do with arrays:"

Example 1.
Fill a 10-element array with numbers from 1 to 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;
         }
     }
 }
Fill a 10-element array with numbers from 10 to 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;
         }
     }
 }
Fill a 10-element array with numbers from 0 to 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;
         }
     }
 }
Fill a 10-element array with numbers from 9 to 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;
         }
     }
 }
Example 2.
Read 10 strings from the keyboard:
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();
      }
   }
 }
Read 10 numbers from the keyboard:
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);
     }
   }
 }
Example 3.
Display an array on the screen:
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]);
        }
     }
 }
Example 4.
Quick (static) initialization. Add up the array elements:
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);
     }
 }
Example 5.
Find the smallest element in an array:
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);
     }
 }
6
Task
New Java Syntax,  6 level,  3 lecture
Locked
String array in reverse order
String array in reverse order
6
Task
New Java Syntax,  6 level,  3 lecture
Locked
Array of numbers in reverse order
Array of numbers in reverse order
Комментарии (2)
  • популярные
  • новые
  • старые
Для того, чтобы оставить комментарий Вы должны sign in
Kirill
Уровень 22
14 December 2022, 22:08
What does it mean? int min = list[0]; why is here 0?
Anonymous #3344466
Уровень 9
6 September 2023, 18:34
Because the first element of the array starts with index 0.