Home  >  Article  >  Java  >  How to assign value to array in java

How to assign value to array in java

王林
王林Original
2019-11-16 11:56:1711996browse

How to assign value to array in java

Use the "=" symbol to assign values ​​to the array

arr[0]=1;      //0代表的是数组的第1个元素 ,元素下标为0
arr[1]=1;      //1代表的是数组的第2个元素 ,元素下标为1

Use loops to assign values ​​to the array

The example is:

 int[ ]  arr=new int[5];     for (int i = 0; i < arr.length; i++) {
            System.out.println("数组第"+(i+1)+"个值为"+i);
         }

Use loop traversal to assign values ​​to the array and output the array

The example is:

 int[ ] arr={1,3,6,5,6,7,84,55,5};  for (int num:arr) {
      System.out.println(num);
  }

Recommended tutorial: Java tutorial

The above is the detailed content of How to assign value to array in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn