HomeContact

Using Array in Java

Published in Java
May 09, 2023
1 min read

🌱Java Array Declaration

String[] codes;
String[] codes = {"JAVA", "CSS", "JS"};
int[] nums = {1, 2, 3, 4};


🌱Accessing Array Elements

Array index numbers with n elements start from zero(0).

// (Example)
// [0] The first element, [1] the second element, ... [n-1] the last element.
public static void main(String[] args) {
String[] codes = {"JAVA", "CSS", "JS"};
System.out.println(codes[0]); // JAVA
}


🌱Change Array Elements

public static void main(String[] args) {
String[] codes = {"JAVA", "CSS", "JS"};
codes[0] = "HTML";
System.out.println(codes[0]); // HTML
}


🌱Array Length

public static void main(String[] args) {
String[] codes = {"JAVA", "CSS", "JS"};
System.out.println(codes.length); // 3
}


🌱Run Array Repeat

public static void main(String[] args) {
String[] codes = {"HTML", "CSS", "JS"};
for (int i = 0; i < codes.length; i++) {
System.out.println(codes[i]); // HTML CSS JS
}
for (String i : codes) {
System.out.println(i); // HTML CSS JS
}
}

Tags

#Java#lang

Share


Previous Article
Why does the logical type 'boolean' in Java have a size of 1 byte instead of 1 bit

Topics

Java
Other
Server

Related Posts

How to use BigInteger in Java - 2
May 31, 2023
1 min