• A Java int array example


     

    Java array FAQ: How do you create an array of Java int values (i.e., a Java “int array”)?

    Answer: There are several ways to define an int array in Java; let’s take a look at a few examples.

    1) Declare a Java int array with initial size; populate it later

    If you know the desired size of your array, an you'll be adding elements to your array some time later in your code, you can define a Java int array using this syntax:

     
    // (1) create a java int array
    int[] intArray = new int[3];
     
    // (2) some time later ... assign elements to the array
    intArray[0] = 1;
    intArray[1] = 2;
    intArray[2] = 3;
     
    // (3) print our java int array
    for (int i=0; i<intArray.length; i++)
    {
      System.out.println(intArray[i]);
    }
    

      

    2) Declare an int array and populate its elements

    Depending on your needs, you can also create your int array using this syntax:

     
    // (1) define your java int array
    int[] intArray = new int[] {4,5,6,7,8};
     
    // (2) print the java int array
    for (int i=0; i<intArray.length; i++)
    {
      System.out.println(intArray[i]);
    }
    

      

    3) A complete Java int array example

    Sometimes it helps to see source code used in a complete Java program, so the Java class/program below demonstrates the different Java int array examples.

    The method named intArrayExample shows our first example. Then, to demonstrate the similiarity between an int array and a String array, the method named stringArrayExample shows how a Java Stringarray is typically created and populated. Finally, the method named intArrayExample2 shows our secondint array example (as shown above).

    Here's the source code for my sample Java class:

    /**
     * Demonstrates several Java array examples, including a
     * Java int array, and a Java String array.
     * Created by Alvin Alexander, <a href="http://alvinalexander.com" title="http://alvinalexander.com">http://alvinalexander.com</a>.
     */
    public class JavaIntArrayExample
    {
     
      public static void main(String[] args)
      {
        new JavaArrayExample();
      }
     
      public JavaArrayExample()
      {
        intArrayExample();
        stringArrayExample();
        intArrayExample2();
      }
     
      /**
       * Create an int array, then populate the array, 
       * and finally print each element in the int array.
       */
      private void intArrayExample()
      {
        int[] intArray = new int[3];
        intArray[0] = 1;
        intArray[1] = 2;
        intArray[2] = 3;
        System.out.println("intArray output");
        for (int i=0; i<intArray.length; i++)
        {
          System.out.println(intArray[i]);
        }
      }
     
      /**
       * Create a String array, then populate the array, 
       * and finally print each element in the int array.
       */
      private void stringArrayExample()
      {
        String[] stringArray = new String[3];
        stringArray[0] = "a";
        stringArray[1] = "b";
        stringArray[2] = "c";
        System.out.println("stringArray output");
        for (int i=0; i<stringArray.length; i++)
        {
          System.out.println(stringArray[i]);
        }
      }
     
      /**
       * Create a Java int array and populate it in one step.
       * Then get the array length and print each element in the array.
       */
      private void intArrayExample2()
      {
        int[] intArray = new int[] {4,5,6,7,8};
        System.out.println("intArray output (version 2)");
        for (int i=0; i<intArray.length; i++)
        {
          System.out.println(intArray[i]);
        }
      }
     
    }
    

      

     
  • 相关阅读:
    Python基础之subprocess
    Python基础之读取ini文件
    Python如何将py文件打包成exe
    C++第四十一篇 -- 安装成功的第一个驱动文件
    C++第四十篇 -- 研究一下Windows驱动开发(三)-- NT式驱动的基本结构
    C++第三十九篇 -- 研究一下Windows驱动开发(二)-- 驱动程序中重要的数据结构
    C++第三十八篇 -- 研究一下Windows驱动开发(二)--WDM式驱动的加载
    C++第三十七篇 -- 调试驱动程序
    iis提示“另一个程序正在使用此文件,进程无法访问。(异常来自HRESULT:0x80070020) ”解决办法
    Serv-U无法上传“中文文件夹”的问题(没有权限)
  • 原文地址:https://www.cnblogs.com/hephec/p/4586784.html
Copyright © 2020-2023  润新知