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

Array_Notes (1)

The document provides an overview of arrays in Java, explaining their structure and usage for storing multiple values of the same type. It includes several example programs demonstrating operations such as summing elements, counting even numbers, finding maximum elements, reversing arrays, and counting occurrences of specific elements. Additionally, it presents code for manipulating larger arrays and separating odd and even numbers.

Uploaded by

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

Array_Notes (1)

The document provides an overview of arrays in Java, explaining their structure and usage for storing multiple values of the same type. It includes several example programs demonstrating operations such as summing elements, counting even numbers, finding maximum elements, reversing arrays, and counting occurrences of specific elements. Additionally, it presents code for manipulating larger arrays and separating odd and even numbers.

Uploaded by

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

Array

Arraysare fundamental structures in Java that allow us to store multiple values of the
same type in a single variable. They are useful for storing and managing collections of
data.
For primitive arrays, elements are stored in a contiguous memory location. For non-
primitive arrays, references are stored at contiguous locations, but the actual objects
may be at different locations in memory.
Searching an element in array
Trace table questions
1) Simple Sum of Elements
public class ArraySum

public static void main(String[] args)

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

int sum = 0;

for (int i = 0; i < arr.length; i++)

sum += arr[i];

System.out.println("The sum is: " + sum);

Answer
2) Counting Even Numbers

public class CountEvenNumbers


{
public static void main(String[] args)
{
int[] arr = {3, 6, 8, 1, 4, 7};
int count = 0;

for (int i = 0; i < arr.length; i++)


{
if (arr[i] % 2 == 0)
{
count++;
}
}

System.out.println("The count of even numbers is: " + count);


}
}
3) Find Maximum Element

public class MaxElement


{
public static void main(String[] args)
{
int[] arr = {10, 20, 5, 40, 30};
int max = arr[0];

for (int i = 1; i < arr.length; i++)


{
if (arr[i] > max)
{
max = arr[i];
}
}

System.out.println("The maximum element is: " + max);


}
}
4)
public class ReverseArray
{
public static void main(String[] args)
{
int[] arr = {1, 2, 3, 4, 5};

for (int i = 0; i < arr.length / 2; i++)


{
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}

System.out.print("Reversed array: ");

for (int i = 0; i < arr.length; i++)


{
System.out.print(arr[i] + " ");
}
}
}
5) Count Occurrences of a Specific Element

public class CountOccurrences


{
public static void main(String[] args)
{
int[] arr = {2, 4, 2, 6, 2, 8};
int target = 2;
int count = 0;

for (int i = 0; i < arr.length; i++)


{
if (arr[i] == target)
{
count++;
}
}

System.out.println("The number of occurrences of " + target + " is: " + count);


}
}
Programs
1) An array called NUMBERS contains 100 numbers. Construct the java code that loops through the
array and stores the square of the number in another array called SQUARES

Answer
public class SquareNumbers
{
public static void main(String[] args)
{
int[] NUMBERS = new int[100]; // Assuming the array is already populated
int[] SQUARES = new int[100];

// Loop through NUMBERS array and store the square in SQUARES array
for (int i = 0; i < NUMBERS.length; i++)
{
SQUARES[i] = NUMBERS[i] * NUMBERS[i];
}

// Print the SQUARES array to check the result


for (int i = 0; i < SQUARES.length; i++)
{
System.out.println("Square of NUMBERS[" + i + "] = " + SQUARES[i]);
}
}
}

2)
An array contains 1000 names. Construct a search algorithm that asks a user to input a name and outputs the
number of times that name appears in the array.
ANSWER
import java.util.Scanner;
public class SearchName
{
public static void main(String[] args)
{
String[] names = new String[1000]; // Assuming the array is already populated
Scanner scanner = new Scanner(System.in);

// Ask the user to input a name


System.out.print("Enter a name to search: ");
String nameToSearch = scanner.nextLine();

// Initialize a counter for occurrences


int count = 0;
// Loop through the array and count occurrences of the name
for (int i = 0; i < names.length; i++) {
if (names[i].equals(nameToSearch)) {
count++;
}
}

// Output the result


System.out.println("The name " + nameToSearch + " appears " + count + " times.");
}
}

3) Construct the pseudocode for a program that loops through an array called “NUMBERS” that contains 500
integers. All the odd numbers should be stored in a ARRAY called “ODDS” and all the even numbers should be stored
in a ARRAY called “EVENS”.

public class SeparateOddEven


{
public static void main(String[] args)
{
int[] NUMBERS = new int[500]; // Assuming the array is already populated

// Count the number of odd and even numbers to define the size of the arrays
int oddCount = 0;
int evenCount = 0;

// First pass to count odd and even numbers


for (int i = 0; i < NUMBERS.length; i++)
{
if (NUMBERS[i] % 2 == 0)
{
evenCount++;
}
else
{
oddCount++;
}
}

// Create arrays to store odd and even numbers


int[] ODDS = new int[oddCount];
int[] EVENS = new int[evenCount];

// Second pass to fill the arrays


int oddIndex = 0;
int evenIndex = 0;
for (int i = 0; i < NUMBERS.length; i++)
{
if (NUMBERS[i] % 2 == 0)
{
EVENS[evenIndex++] = NUMBERS[i];
}
else
{
ODDS[oddIndex++] = NUMBERS[i];
}
}

// Print the odd numbers


System.out.println("Odd numbers:");
for (int i = 0; i < ODDS.length; i++)
{
System.out.println(ODDS[i]);
}

// Print the even numbers


System.out.println("Even numbers:");
for (int i = 0; i < EVENS.length; i++)
{
System.out.println(EVENS[i]);
}
}
}

You might also like