zoukankan      html  css  js  c++  java
  • 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]);
        }
      }
     
    }
    

      

     
  • 相关阅读:
    人工智能,人工神经网络,机器学习,深度学习,卷积神经网络 时间线与内在联系(转载)
    基于spark logicplan的表血缘关系解析实现
    spark ml pipeline构建机器学习任务
    常用特征离散化方法
    spark sql插入表时的文件个数研究
    Spark累加器(Accumulator)
    java中使用URLClassLoader访问外部jar包的java类
    Scala里面的排序函数的使用
    Spark获取DataFrame中列的几种姿势--col,$,column,apply
    spark.sql.shuffle.partitions和spark.default.parallelism的区别
  • 原文地址:https://www.cnblogs.com/hephec/p/4586784.html
Copyright © 2011-2022 走看看