search
HomeJavaJavaBaseWhat are the methods of assigning values ​​to arrays in java?

What are the methods of assigning values ​​to arrays in java?

Nov 21, 2019 pm 04:42 PM
javaarraymethodAssignment

What are the methods of assigning values ​​to arrays in java?

1. Direct assignment

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

2. Use for loop assignment

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

Result:

What are the methods of assigning values ​​to arrays in java?

3. Use loop traversal to assign values ​​to the array

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

Result:

What are the methods of assigning values ​​to arrays in java?

Recommended tutorial: Java introductory tutorial

The above is the detailed content of What are the methods of assigning values ​​to arrays 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)