0% found this document useful (0 votes)
3 views

Pre-read - Arrays

Arrays are fundamental data structures used to store multiple values of the same type, with a fixed size and indexed access. Different programming languages like Java, Python, C#, and C++ have their own syntax for declaring and iterating over arrays, as well as handling dynamic arrays. The document also explains how to find the maximum element in an array using a straightforward iterative approach.

Uploaded by

nishi_nkm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Pre-read - Arrays

Arrays are fundamental data structures used to store multiple values of the same type, with a fixed size and indexed access. Different programming languages like Java, Python, C#, and C++ have their own syntax for declaring and iterating over arrays, as well as handling dynamic arrays. The document also explains how to find the maximum element in an array using a straightforward iterative approach.

Uploaded by

nishi_nkm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Basics of Arrays

Arrays are fundamental data structures in programming, used to store multiple values of the
same type in a single variable. An array is a collection of elements, each identified by at least
one array index or key. The array's size is fixed upon creation, meaning the number of elements
it can hold is set when it's declared. Arrays are useful for storing data that is naturally structured
in a tabular format, like a list of items of the same type, and they provide quick access to their
elements via their indices.

Arrays have several key features:

1.​ Fixed Size: Once an array's size is set, it cannot be changed.


2.​ Homogeneous Elements: All elements in the array must be of the same data type.
3.​ Indexed: Each element in the array can be accessed using its numeric index.
4.​ Contiguous Memory Allocation: In most languages, arrays are stored in contiguous
blocks of memory, which makes access to elements efficient.

Declaring Arrays in Different Programming Languages

Java

In Java, arrays are declared by specifying the type of elements they will hold, followed by
square brackets. Here's an example of declaring an array of integers:

int[] myArray;
myArray = new int[10]; // Initializes an array to hold 10 integers

Python

Python uses lists, which are more flexible than arrays but serve similar purposes. Lists in Python
do not require declaration of size or type ahead of time:

my_list = [] # An empty list


my_list = [1, 2, 3] # A list with three integers

C#

In C#, arrays are declared similarly to Java, but the type and the size can be declared at once:

int[] myArray = new int[10]; // Declares and initializes an array for


10 integers
C++

In C++, arrays are declared by specifying the type, followed by the size in square brackets:

int myArray[10]; // Declares an array of 10 integers

Iterating Over Arrays

Iterating over arrays involves accessing each element in the array sequentially. Here's how you
can iterate over arrays in different programming languages:

Java

You can use a traditional for loop or an enhanced for loop (also known as a "for-each" loop):

int[] myArray = {1, 2, 3, 4, 5};


// Traditional for loop
for (int i = 0; i < myArray.length; i++) {
System.out.println(myArray[i]);
}
// Enhanced for loop
for (int element : myArray) {
System.out.println(element);
}

Python

In Python, iteration is straightforward using a for loop, as Python's lists are inherently iterable:

my_list = [1, 2, 3, 4, 5]
for element in my_list:
print(element)

C#

C# supports both traditional for loops and foreach loops:


int[] myArray = {1, 2, 3, 4, 5};
// Traditional for loop
for (int i = 0; i < myArray.Length; i++) {
Console.WriteLine(myArray[i]);
}
// Foreach loop
foreach (int element in myArray) {
Console.WriteLine(element);
}

C++

C++ also supports traditional for loops and range-based for loops (since C++11):

int myArray[] = {1, 2, 3, 4, 5};


// Traditional for loop
for (int i = 0; i < 5; i++) {
cout << myArray[i] << endl;
}
// Range-based for loop (C++11 and later)
for (int element : myArray) {
cout << element << endl;
}

Dynamic Arrays

Java

In Java, dynamic arrays can be handled using ArrayList:

ArrayList<Integer> dynamicArray = new ArrayList<>();


dynamicArray.add(1); // Adding elements dynamically
dynamicArray.add(2);

Python
Python lists are inherently dynamic:

my_list.append(6) // Adding an element to the end of the list

C#

In C#, List<T> is used for dynamic arrays:

List<int> dynamicList = new List<int>();


dynamicList.Add(1); // Adding elements dynamically

C++

In C++, std::vector is the equivalent of a dynamic array:

vector<int> myVector;
myVector.push_back(1); // Adding elements dynamically

Each language provides its own tools and libraries to manage dynamic arrays efficiently,
allowing for operations like resizing, insertion, and deletion.

Find maximum element from array

Java

int[] myArray = {1, 2, 3, 4, 5};


int max = myArray[0]; // Assume the first element is the max
initially
for (int i = 1; i < myArray.length; i++) {
if (myArray[i] > max) {
max = myArray[i]; // Update max if current element is greater
}
}
System.out.println("Max: " + max);
Python

my_list = [1, 2, 3, 4, 5]
max_value = my_list[0] # Assume the first element is the max
initially
for i in range(1, len(my_list)):
if my_list[i] > max_value:
max_value = my_list[i] # Update max if current element is
greater
print("Max:", max_value)

C#

int[] myArray = {1, 2, 3, 4, 5};


int max = myArray[0]; // Assume the first element is the max
initially
for (int i = 1; i < myArray.Length; i++) {
if (myArray[i] > max) {
max = myArray[i]; // Update max if current element is greater
}
}
Console.WriteLine("Max: " + max);

C++

int myArray[] = {1, 2, 3, 4, 5};


int max = myArray[0]; // Assume the first element is the max
initially
for (int i = 1; i < 5; i++) {
if (myArray[i] > max) {
max = myArray[i]; // Update max if current element is greater
}
}
cout << "Max: " << max << endl;

In each example, we start by assuming the first element is the maximum, and then we check
each subsequent element to see if it is greater than the current maximum. If it is, we update the
maximum. This approach ensures that we only iterate through the array once, providing an
efficient way to find the maximum value.

You might also like