Skip to content

Commit 575349c

Browse files
committed
feat: Illustrate array declaration, initialization, element update, and increment behavior
🧠 Logic: - Declares arrays in various formats: with size, inline initialization, and deferred allocation. - Updates B[2] = 15 to demonstrate element modification. - Uses a for loop to print and increment each element of B (post-increment prints original value). - Uses a for-each loop with post-increment (x++), which increments a copy, not the actual array. - Comments explain behavior clearly for learning purposes. Signed-off-by: Somesh diwan <[email protected]>
1 parent a9990cf commit 575349c

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,25 @@
11
public class ArrayAB {
22
public static void main(String[] args) {
3-
int A[] = new int[10];
3+
int A[] = new int[10]; // Creates an array of size 10 with default values (0)
4+
int B[] = {1,2,3,4,5}; // Creates and initializes an array with specific values
5+
int C[]; // Declares array reference
6+
C = new int[10]; // Initializes array C with size 10
7+
int [] D = new int[5]; // Alternative syntax for array declaration
48

5-
int B[] ={1,2,3,4,5};
6-
7-
int C[];
8-
9-
C=new int[10]; //Three different methods of declaring array.
10-
11-
int [] D = new int[5];
12-
13-
//How to access the ellements
14-
15-
B[2]=15;
9+
B[2] = 15; // Changes the third element (index 2) of array B to 15
1610

1711
/*for(int i=0; i<B.length; i++)
1812
{
1913
System.out.println(B[i]);
2014
}*/
2115

22-
//Counter controoled for loop
23-
for(int i=0;i<B.length; i++)
24-
{
16+
//Counter-controlled for loop
17+
for(int i=0;i<B.length; i++) {
2518
System.out.println(B[i]++);
2619
}
2720

28-
for(int x:B)
29-
{
21+
for(int x:B) {
3022
System.out.println(x++);
3123
}
32-
3324
}
34-
}
25+
}

0 commit comments

Comments
 (0)