An array is a data structure that stores a collection of elements of the same data type. Each element in an array is identified by an index, which is a number that represents its position in the array.
Some of the most common operations that can be performed on arrays include:
-
Initializing an array: This involves declaring an array variable and assigning it a set of values.
-
Accessing array elements: This involves retrieving the value of an element in the array using its index.
-
Modifying array elements: This involves changing the value of an element in the array using its index.
-
Finding the length of an array: This involves determining the number of elements in the array.
-
Looping through array elements: This involves iterating over the elements in an array and performing an operation on each element.
-
Sorting array elements: This involves arranging the elements in an array in ascending or descending order.
-
Searching for a specific element in an array: This involves finding the index of an element in the array that matches a specified value.
Arrays are a fundamental data structure in programming, and many programming languages provide built-in support for arrays. Some examples of programming languages that support arrays include Java, C++, Python, and JavaScript.
public class Main {
public static void main(String[] args) {
String[] progLang = {"Java", "C++", "Python", "Perl", "Lisp"};
int[] numbers = {1, 3, 5, 25, 5000};
System.out.println("Language at Pos 0: " + progLang[0]);
System.out.println("Number at Pos 0: " + numbers[0]);
System.out.println("Language at Pos 4: " + progLang[4]);
//Change elements
progLang[0] = "Prolog";
System.out.println("Language at Pos 0: " + progLang[0]);
numbers[0] = 10000;
System.out.println("Number at Pos 0: " + numbers[0]);
//Length of Array - Number of elements:
System.out.println("Num of ProgLangs: " + progLang.length);
System.out.println("Num of Numbers: " + numbers.length);
for (String x: progLang) {
System.out.println(x);
}
}
}
Here's a detailed explanation of each block of code in our Java program:
public class Main {
This line defines a public class called "Main". This is the main class that contains the main method.
public static void main(String[] args) {
This line defines the main method. This method is executed when the program is run.
// create arrays
String[] progLang = {"Java", "C++", "Python", "Perl", "Lisp"};
int[] numbers = {1, 3, 5, 25, 5000};
These lines create two arrays: "progLang" and "numbers". The "progLang" array contains a list of programming language names, and the "numbers" array contains a list of numbers.
// access elements of arrays
System.out.println("Language at Pos 0: " + progLang[0]);
System.out.println("Number at Pos 0: " + numbers[0]);
System.out.println("Language at Pos 4: " + progLang[4]);
These lines access elements of the "progLang" and "numbers" arrays using their indices. The first line prints the first element of the "progLang" array, the second line prints the first element of the "numbers" array, and the third line prints the fifth element of the "progLang" array.
// modify elements of arrays
progLang[0] = "Prolog";
System.out.println("Language at Pos 0: " + progLang[0]);
numbers[0] = 10000;
System.out.println("Number at Pos 0: " + numbers[0]);
These lines modify elements of the "progLang" and "numbers" arrays using their indices. The first line changes the first element of the "progLang" array to "Prolog", and the second line changes the first element of the "numbers" array to 10000. The following two lines print out the updated elements.
// get length of arrays
System.out.println("Num of ProgLangs: " + progLang.length);
System.out.println("Num of Numbers: " + numbers.length);
These lines use the "length" property of the arrays to print out the number of elements in each array.
// loop through array elements
for (String x: progLang) {
System.out.println(x);
}
This is a for-each loop that iterates over the elements of the "progLang" array and prints each element to the console. The loop iterates over each element in the array and assigns each element to the variable "x". The code inside the loop then executes for each element.
No comments:
Post a Comment