java arrays Flashcards

1
Q

How to declare an array?

(Initialize and populate at the same time)

A

String[] daysOfWeek = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};

int[] nums = {1, 2, 4, 8, 16, 200};

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

You can change an existing value of an array

A

int[] nums = {1, 2, 4, 8, 16, 200};

System.out.println(nums[0]); // 1

System.out.println(nums[4]); // 16

// You can change an existing value nums[2] = 99;

System.out.println(nums[2]);

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

How to declare an array?

Declaring and Assigning

A

// Method 1: Declaring and Assigning (or just simply initialize an array)

String[] daysOfWeek; // Declared an array

daysOfWeek = new String[7]; // Assign a value to how many items will be in array

// String[] weekDays = new String[7];

// Initialize a String array

// Next, we populate our array

daysOfWeek[0] = “Monday”;

daysOfWeek[1] = “Tuesday”;

daysOfWeek[2] = “Wednesday”;

daysOfWeek[3] = “Thursday”;

daysOfWeek[4] = “Friday”;

daysOfWeek[5] = “Saturday”;

daysOfWeek[6] = “Sunday”;

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

Accessing an array

A

**// Finally, we need to learn how to access an array

System.out.println(daysOfWeek[0]); // Monday

// System.out.println(daysOfWeek[7]); // If uncommented, will give an error - ArrayIndexOutOfBoundException**

// How to declare an array?

**// Method 1: Declaring and Assigning (or just simply initialize an array)

String[] daysOfWeek; // Declared an array
daysOfWeek = new String[7]; // Assign a value to the String array

// String[] weekDays = new String[7]; // Initialize a String array**

// Next, we populate our array
daysOfWeek[0] = “Monday”;
daysOfWeek[1] = “Tuesday”;
daysOfWeek[2] = “Wednesday”;
daysOfWeek[3] = “Thursday”;
daysOfWeek[4] = “Friday”;
daysOfWeek[5] = “Saturday”;
daysOfWeek[6] = “Sunday”;

// Method 2: Initialize and populate at the same time
String[] daysOfWeek = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};

System.out.println(daysOfWeek[6]);

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

Checking the length of the array

A

System.out.println(nums.length); // This is the length of the array

int[] nums = {1, 2, 4, 8, 16, 200};

System.out.println(nums[0]); // 1
System.out.println(nums[4]); // 16

 // You can change an existing value
 nums[2] = 99;

System.out.println(nums[2]);

// nums[5] = 100; // if uncommented, will give an error because you can’t access beyond the array length

 // Once the array length is defined, you CANNOT change the size of the array
 // You cannot add or remove elements from an array.
 // Note this does not mean that you can't go and change the size when you declare it,
 // it means that the size of the array will not change programmatically.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If you want to print the contents of an array

A
**// If you want to print the contents of an array, we use a method toString from the Arrays class
 System.out.println(Arrays.toString(nums));**
// How to print the contents of an array?
 int[] nums = {1, 2, 4, 8, 16, 200};
System.out.println(nums); // sysout does not print the contents of an array
 // It prints [ to say it is an array, then I to say its of type Integer, then @??? to show the memory location
 // If you want to print the contents of an array, we use a method toString from the Arrays class
 System.out.println(Arrays.toString(nums));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Method 1: Initializing Arrays

A
**// Method 1: Initialize
 //index 0 1 2
 int[] numbers = {1, 2, 3};

System.out.println(Arrays.toString(numbers));
System.out.println(numbers.length); // count = 3

System.out.println(numbers[2]);

 // Method 2: Initialize then populate later
 String[] daysOfWeek = new String[7];
 System.out.println(Arrays.toString(daysOfWeek));

daysOfWeek[0] = “Monday”;
daysOfWeek[1] = “Tuesday”;
daysOfWeek[2] = “Wednesday”;
daysOfWeek[3] = “Thursday”;
daysOfWeek[4] = “Friday”;
daysOfWeek[5] = “Saturday”;
daysOfWeek[6] = “Sunday”;**

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

Method 2: Initialize array then populate later

A

**// Method 2: Initialize then populate later
String[] daysOfWeek = new String[7];
System.out.println(Arrays.toString(daysOfWeek));

daysOfWeek[0] = “Monday”;
daysOfWeek[1] = “Tuesday”;
daysOfWeek[2] = “Wednesday”;
daysOfWeek[3] = “Thursday”;
daysOfWeek[4] = “Friday”;
daysOfWeek[5] = “Saturday”;
daysOfWeek[6] = “Sunday”;**

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

initializing an array and a variable

A

int[] numbers = {1, 2, 3}, heights = new int[5]; // Both numbers and heights must be int arrays

int nums[] = {10, 20, 30}, x = 5; // nums is an int array and x is an int variable

System.out.println(Arrays.toString(numbers));
System.out.println(Arrays.toString(nums));
System.out.println(Arrays.toString(heights));
System.out.println(x);

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

Can we delete elements from an array?

A

// Can we delete elements from an array?

int[] nums = {1,2,3,4};
nums[2] = 0; // Reset an int value to its default value

System.out.println(Arrays.toString(nums));

String[] names = {“Omar”, “Jane”};
names[0] = null; // Strings are objects, and their default value is null
System.out.println(Arrays.toString(names));

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

Practice with arrays

A
**// Practice
 // int[] nums = new int[5]; // Creating an empty array
 // Another way
 int[] nums;
 nums = new int[5];

System.out.println(Arrays.toString(nums));

 // Assigning the values
 nums[0] = 55;
 nums[1] = 443;
 nums[2] = 12;
 nums[3] = 5;
 nums[4] = 387;

System.out.println(Arrays.toString(nums));

 // Changing the value of the third element
 nums[2] = 99;
 System.out.println(Arrays.toString(nums));**
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Practice with arrays part 2

A

**// Practice - Part 2
int[] nums = {55, 443, 12, 5, 387};

System.out.println(Arrays.toString(nums));

 // Changing the value of the third element
 nums[2] = 99;
 System.out.println(Arrays.toString(nums));**
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Class Practice 3

A

Scanner scan = new Scanner(System.in);

// Task
int[] nums = new int[5];

**System.out.println(“Please enter five numbers: “);

 // Method 1: Redundant
// int userInput;
// userInput = scan.nextInt();
// nums[0] = userInput;
// userInput = scan.nextInt();
// nums[1] = userInput;
// userInput = scan.nextInt();
// nums[2] = userInput;
// userInput = scan.nextInt();
// nums[3] = userInput;
// userInput = scan.nextInt();
// nums[4] = userInput;
 // Method 2: Good!
 nums[0] = scan.nextInt();
 nums[1] = scan.nextInt();
 nums[2] = scan.nextInt();
 nums[3] = scan.nextInt();
 nums[4] = scan.nextInt();
 // Method 3: Interesting - If you use it, do not forget to comment the first line int[] nums
// int[] nums = {scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt()};

System.out.println(Arrays.toString(nums));

int sum = nums[0] + nums[1] + nums[2] + nums[3] + nums[4];**

System.out.println(sum);

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

Class Practice 4

A

**String[] roles = {“Developer”, “Tester”, “Scrum Master”, “BA”, “Product Owner”};

System.out.println(“Please enter a number between 0 and 4: “);
int select = scan.nextInt();

System.out.println(roles[select]);**

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

how to print arrays

A

int[] nums = {-5, 2, 23, -10}; // Arrays.toString() System.out.println(Arrays.toString(nums));

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

sorting arrays

A

int[] nums = {-5, 2, 23, -10};

// Arrays.toString()
System.out.println(Arrays.toString(nums));

**// Arrays.sort()
Arrays.sort(nums); // You have to sort first and then print - Since nums is numerical, it sorts in an ascending order
System.out.println(Arrays.toString(nums)); // nums is sorted and remains sorted

// System.out.println(Arrays.toString(Arrays.sort(nums))); // If uncommented, it will not work.

 // sort method also works with Strings
 String[] cities = {"Moscow", "DC", "Cairo", "Baku"};
 Arrays.sort(cities); // sorted based on the natural order of Strings which is the alphabetical order
 System.out.println(Arrays.toString(cities));**
17
Q

Arrays.equals()

A

// Arrays.equals()
int[] nums = { -5, 2, 23, -10 };
int[] numbers = { -5, 2, 23, -10 };

System.out.println(nums == numbers); // false because they are objects and == will only compare the addresses not contents
System.out.println(Arrays.equals(nums, numbers)); // true

18
Q

Arrays.equals() 2

A

// Arrays.equals()
int[] nums = { -5, 2, 23, -10 };
int[] numbers = { 2, 23, -10, -5 };

**System.out.println(Arrays.equals(nums, numbers)); // false - because the arrays are not equal
 // equal arrays means every element is equal to the corresponding element in the other array**
19
Q

Arrays.copyOf()

A

// Arrays.copyOf()
int[] numbers = { 5, -1, 12, 500, 2 };

System.out.println(Arrays.toString(numbers));

**numbers = Arrays.copyOf(numbers, 10); // This tells Java to copy the old array into a new bigger array of size
 // 10
 System.out.println(Arrays.toString(numbers));

numbers = Arrays.copyOf(numbers, 4); // If the new array is shorter than the old one, Java will truncate the old one
System.out.println(Arrays.toString(numbers));**

20
Q

two-dimensional arrays

A

// Method 1: Initialize the Array with Known values

// Columns: 0 1 2
int[][] nums = { { 10, 20, 30 }, // row 0
{ 40, 50, 60 }, // row 1
{ 70, 80, 90 }, // row 2
{ 100, 200, 300 } }; // row 3

int[][] numbers = { { 0, 1 }, { 2, 3 } };

// To print two-dimensional arrays,
System.out.println(nums); // Will print the address
System.out.println(Arrays.toString(nums)); // Addresses of each row of the arrays of array
System.out.println(Arrays.deepToString(nums)); // This will print all the elements of the array
System.out.println(Arrays.deepToString(numbers));

**// We can do the same thing with Strings
 // Columns: 0 1 2
 String[][] veggies = {{"Tomato", "Cucumber", "Eggplant"}, // row 0
 {"Potato", "Cauliflower", "Spinach"}}; // row1

System.out.println(Arrays.deepToString(veggies));**

21
Q

accessing elements in a multidimensional array

A
**// How to access elements in our array?
 // Whenever you want to access an element, we list the row first. Always the row first
 // arrayName[row][column]
 System.out.println(veggies[1][1]);

veggies[1][2] = “Garlic”;
System.out.println(Arrays.deepToString(veggies));**

22
Q

Big notes multidimensional arrays

A

**// NOTES TIME

 // Note 1: Similar to the one-dimensional array, you can declare a two-dimensional array as follows:
 int[][] numbers = new int[3][4];
 int nums[][] = new int[3][4];
 // Note 2: You can declare something called a Ragged array.
 // A Ragged array is an array that does not have the same number of elements in each row

String[][] fruits = {{“Apple”, “Banana”, “Orange”},
{“Pineapple”},
{“Peach”, “Kiwi”}};

System.out.println(Arrays.deepToString(fruits));

 // Note 3: Since we do not need a fixed number of columns, then we do not need to declare it
 int[][] temps = new int[3][]; // You can't leave the number of rows blank**
**// Note 4: The elements of an array of arrays are basically arrays!
 // This means we can put some arrays together to form a 2d array
 String arr1[] = {"Apple", "Pear"};
 String arr2[] = {"Mango", "Banana"};
 String[][] niceFruits = {arr1,
 arr2};
 System.out.println(Arrays.deepToString(niceFruits));**
**System.out.println(Arrays.toString(niceFruits[0])); // Accessing the first element in the array or arrays which
 // returns an array, and I can print it using Arrays.toString(), not the deep one

// Note 5: Finding the length of a 2D array

System.out.println(fruits.length); // return 3 for the number of rows in fruits

 // How to find the length of each row then?
 System.out.println(fruits[0].length); // 3
 System.out.println(fruits[1].length); // 1
 System.out.println(fruits[2].length); // 2**
23
Q

Arrays Class Methods

A

Arrays.toString(); → converts given array to a string
○ String a = Arrays.toString(counties);

● Arrays.sort(); → sorts the complete array in ascending order
○ Arrays.sort(counties);

● Arrays.equals(); → checks if both the arrays are equal or not
○ Arrays.equals(arrayfr, myArr2);

● Arrays.copyOf(); → creates new array using a copy of another array. You can specify the new size.
○ String allCities[] = Arrays.copyOf(citiesArr, 10); //10 → specifies the size