Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Arrays

Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.

To declare an array, define the variable type with square brackets:

String[] cars;

We have now declared a variable that holds an array of strings. To insert values to it, we can
use an array literal - place the values in a comma-separated list, inside curly braces:

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of integers, you could write:

int[] myNum = {10, 20, 30, 40};

It is called Horizontal Implementation with initialized values of elements.

In order to see the output of that array:

public class ArrayExStringInt


{
public static void main(String[] args)
{
// This is declaration of String Array with 4 elements
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"}; // Horizontal because of this style

// This is declaration of Integer Array with 4 elements


int[] myNum = {10, 20, 30, 40}; // Horizontal because of this style

System.out.println(cars[0]); // To display the value of in String Array at 0 index.


System.out.println(myNum[0]); // To display the value of in Integer Array at 0 index.
}
}
Sample Output:

Volvo
10
Vertical Implementation:

public class ArrayStringIntVerticalImplementation


{
public static void main(String[] args)
{
//String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
//int[] myNum = {10, 20, 30, 40};

String[] cars = new String[4]; // declaration of String Array with 4 elements

cars[0] = "Volvo"; // starts at index 0


cars[1] = "BMW"; // In vertical implementation you will see index and values at the same time.
cars[2] = "Ford"; // Vertical implementation because of this style declaring indexes and values.
cars[3] = "Mazda";

System.out.println(cars[3]); // display the value at index 3 of array

int[] myNum = new int[4];

myNum[0] = 10; // starts at index 0


myNum[1] = 20; // same as above comments
myNum[2] = 30;
myNum[3] = 40;

System.out.println(myNum[3]); // display the value at index 3 of array


}
}

Sample Output:

Mazda
40
Accessing Arrays Using For Loop Statements

public class ArrStrIntVerForLoop


{
public static void main(String[] args)
{
//String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
//int[] myNum = {10, 20, 30, 40};

String[] cars = new String[4]; // declaration of String Array with 4 elements

cars[0] = "Volvo"; // starts at index 0


cars[1] = "BMW"; // In vertical implementation you will see index and values at the same time.
cars[2] = "Ford";
cars[3] = "Mazda";

for(int ctr = 0;
ctr < 4; ctr = ctr + 1) // the counting starts at 0 or zero because of index
System.out.println(cars[ctr]);

System.out.println(); // Separation or Space

int[] myNum = new int[4];

myNum[0] = 10; // starts at index 0


myNum[1] = 20;
myNum[2] = 30;
myNum[3] = 40;

for(int ctr = 0; ctr < 4; ctr = ctr + 1) // the counting starts at 0 or zero because of index
System.out.println(myNum[ctr]); // for Vertical Implementation

System.out.println(); // Separation or Space

String[] cars2 = {"Volvo", "BMW", "Ford", "Mazda"}; // for Horizontal Implementation

for(int ctr = 0; ctr < 4; ctr = ctr + 1) // the counting starts at 0 or zero because of index
System.out.print(cars2[ctr]+” ”); // Vertical and Horizontal implementation have the same
// For Loop Statements in different Arrays.
}
}

Sample Output:

Volvo
BMW
Ford
Mazda

10
20
30
40

Volvo BMW Ford Mazda


Problem Definition

1. Create a program that initialized String Array which have the following values:
cars [] = {"Volvo", "BMW", "Ford", "Mazda"}; then display output of Array.

Algorithm:
Initialize: cars [] = {"Volvo", "BMW", "Ford", "Mazda"};
Output: cars[0], cars[1], cars[2], cars[3];

Program Design

Start

cars [] = {"Volvo",
"BMW", "Ford",
"Mazda"};

Print(cars[0])

Print(cars[1])

Print(cars[2])

Print(cars[3])

Stop

Program Coding
Program Testing and Debugging

Sample Output:

Problem Definition

2. Create a program that initialized String Array which have the following values:
cars [] = {"Volvo", "BMW", "Ford", "Mazda"}; then display output of Array,
using Pre-condition Approach Loop

Algorithm:
Initialize: cars [] = {"Volvo", "BMW", "Ford", "Mazda"};
Process: i = 0, i = i + 1
Loop: i < 4
Output: cars[i]

Program Design

Start

A
A

cars [] = {"Volvo",
"BMW", "Ford",
"Mazda"};

i=0

False
i<4 Stop

True

Print(cars[i])

Loop
i=i+1

Program coding

Program Testing and Debugging


Sample Output:
Problem Definition

3. Create a program that accept how many elements can user put on Array of Car brand. Print the elements in Car Array.

Algorithm:

Input: i, c, cars[i];

Process: cars[] = new String[c], c = 0, cars[Ii], i = 0, i = i + 1, i = i + 1,

Loop: i < c, I < c

Print: cars[i]

Program Design
Start

i = 0, c = 0;

Input(c)

cars[] = new String[c]

i=0

False
i<c

True
i=0
Input(cars[i])

Loop False
i=i+1 i<c

True

Print(cars[i]) Stop

Loop
i=i+1
Program Coding
Program Testing and Debugging

You might also like