0% found this document useful (0 votes)
2 views15 pages

2.2arrays in Java

The document provides an overview of arrays in Java, explaining their structure, types (rectangular and jagged), and how to declare and initialize them. It also covers multi-dimensional arrays, variable-length arguments (varargs), and command-line arguments, illustrating these concepts with code examples. The content is aimed at helping users understand how to effectively use arrays and related features in Java programming.

Uploaded by

sanskariop79
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views15 pages

2.2arrays in Java

The document provides an overview of arrays in Java, explaining their structure, types (rectangular and jagged), and how to declare and initialize them. It also covers multi-dimensional arrays, variable-length arguments (varargs), and command-line arguments, illustrating these concepts with code examples. The content is aimed at helping users understand how to effectively use arrays and related features in Java programming.

Uploaded by

sanskariop79
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Arrays in Java

• An array in Java is a data structure that stores multiple values


of the same type in a single variable, instead of declaring
separate variables for each value.
• Array stores many values in one variable.
• Values must be of same type.
• Each value is accessed using a number (index).
• Index starts from 0.
Array types
Rectangular and Jagged Arrays
• A rectangular array is a multidimensioned array in
which all of the rows have the same number of
elements and all of the columns have the same number
of elements. Rectangular arrays model rectangular
tables exactly.
• A jagged array is one in which the lengths of the rows
need not be the same. For example, a jagged matrix
may consist of three rows, one with 5 elements, one
with 7 elements, and one with 12 elements
• C, C++, and Java support jagged arrays but not
rectangular arrays.
Declaration and Initialization

Declaration
int[] numbers; // Preferred
String names[]; // Also valid
Initialization:
numbers = new int[5]; // array of 5 integers, default values = 0
//combine declaration and initialization
int[] numbers = {10, 20, 30, 40, 50}; // length = 5
public class SimpleArrayExample {
public static void main(String[] args) {
// Create and initialize an array
int[] numbers = {5, 10, 15, 20, 25};

// Print all values using a loop


for (int i = 0; i < [Link]; i++) {
[Link]("Element at index " + i +
": " + numbers[i]);
} }
}
Multi-Dimensional Arrays in Java (2D Arrays)

• A multidimensional array is an array of arrays.


• Multidimensional arrays are useful when you
want to store data as a tabular form, like a
table with rows and columns.
• To create a two-dimensional array, add each
array within its own set of curly braces:

int[][] matrix;
• Initialization with values:
• java
• int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
public class TwoDArrayExample {
public static void main(String[] args) {
int[][] matrix = {
{5, 10, 15},
{20, 25, 30},
{35, 40, 45}
};

[Link]("Matrix Output:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < matrix[i].length; j++) {
[Link](matrix[i][j] + "\t");
}
[Link]();
}
}
}
Variable-Length Argument List
(Varargs) in Java
• In Java, Variable Arguments (Varargs) write
methods that can take any number of inputs,
which simply means we do not have to create
more methods for different numbers of
parameters. This concept was introduced in
Java 5 to make coding easier. Instead of
passing arrays or multiple methods, we can
simply use one method, and it will handle all
the test cases.
• Syntax:
• returnType methodName(type...
variableName)

• Use ... after the data type to declare a varargs


parameter.
• Inside the method, the varargs is treated as an
array.
• // Error: varargs must be last
void example(int... nums, String name) { }
//correct
void example(String name, int... nums) { }
public class VarArgsExample {

// Method with variable-length arguments


static void printNumbers(int... numbers) {
for (int num : numbers) {
[Link](num + " ");
} output:
[Link](); // For new line
}

public static void main(String[] args) {


printNumbers(10); // One argument
printNumbers(1, 2, 3, 4, 5); // Multiple arguments
printNumbers(); // No arguments
}
}
Command-Line Argument List in Java

• Java command-line argument is an argument, i.e.,


passed at the time of running the Java program. In
Java, command-line arguments passed from the
console can be received by the Java program and
used as input. The users can pass the arguments
during the execution, bypassing the command-line
arguments inside the main() method.
Hello World
• Note: Here, the words Hello and World are the
command-line arguments. JVM will collect these
words and will pass these arguments to the main
method as an array of strings called args. The JVM
passes these arguments to the program inside
args[0] and args[1].
public class xyz {
public static void main(String[] args) {
for (int i = 0; i < [Link]; i++) {
[Link](args[i]);
}
}
}

You might also like