"ต่อไปนี้คือตัวอย่างบางส่วนของสิ่งที่น่าสนใจที่คุณสามารถทำได้ด้วยอาร์เรย์:"

ตัวอย่างที่ 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 ลงทะเบียนสำหรับหลักสูตรเต็ม