"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);
    }
}

A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


undefined
7
Task
New Java Syntax, level 7, lesson 3
Locked
Welcome! But not everyone.
This is the signIn() method that greets website users. Currently, it greets all users, but it should only greet registered users. All unregistered users have the name "user". Add username validation at the beginning of the signIn() method. If the name is "user", use the return keyword to abort execu
undefined
7
Task
New Java Syntax, level 7, lesson 3
Locked
A cube calculator
Let's implement a calculator that will compute the powers of numbers. To do this, create a cube() method. It must take a long integer value as an argument. The method should raise the passed value to the third power and return it as the method's result. The numbers you have to work with may be large
undefined
7
Task
New Java Syntax, level 7, lesson 3
Locked
A cubed cube calculator
You won't blow any minds by unveiling a calculator that can raise a number to the power of three. But a calculator that can raise a number to the power of nine is another matter! So let's implement one! To do this, create a public static long ninthDegree(long) method. It must take a long integer val