Lesson 2 Flashcards

(12 cards)

1
Q

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

A

Arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Advantages of Array

A

•Code Optimization
•Random access

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Disadvantages of Array

A

Size Limit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Types of Array in java

A

•Single Dimensional Array
•Multidimensional Array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Array Declaration

A

dataType[] arrayRefVar;
//preferredway.
or
dataType arrayRefVar[];
// works but not preferred way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example of Declaration of array

A

double[] myList; // preferred way.
or
double myList[]; // works but not preferred way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Array Initialization example

A

// declare an array
int[] age = new int[5];
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Example: Access Array Elements

A

class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};
// access each array elements
System.out.println(“Accessing Elements of Array:”);
System.out.println(“First Element: “ + age[0]);
System.out.println(“Second Element: “ + age[1]);
System.out.println(“Third Element: “ + age[2]);
System.out.println(“Fourth Element: “ + age[3]);
System.out.println(“Fifth Element: “ + age[4]);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Example: Using For Loop
Output:
12
4
5

A

class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array
// using for loop
System.out.println(“Using for Loop:”);
for(int i = 0; i < age.length; i++) {
System.out.println(age[i]);
} } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Example: Using the for-each Loop
Output:
12
4
5

A

Output class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array
// using for loop
System.out.println(“Using for-each Loop:”);
for(int a : age) {
System.out.println(a);
} } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Creating Array
Syntax

A

arrayRefVar = new dataType[arraySize];

dataType[] arrayRefVar = new dataType[arraySize];
Alternatively you can create arrays as follows −
dataType[] arrayRefVar = {value0, value1, …, valuek};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Processing Arrays
The foreach Loops
Example:

A

public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (double element: myList) {
System.out.println(element);
} } }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly