java arrays Flashcards
How to declare an array?
(Initialize and populate at the same time)
String[] daysOfWeek = {“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”};
int[] nums = {1, 2, 4, 8, 16, 200};
You can change an existing value of an 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]);
How to declare an array?
Declaring and Assigning
// 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”;
Accessing an array
**// 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]);
Checking the length of the array
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.
If you want to print the contents of an array
**// 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));
Method 1: Initializing Arrays
**// 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”;**
Method 2: Initialize array then populate later
**// 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”;**
initializing an array and a variable
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);
Can we delete elements from an array?
// 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));
Practice with arrays
**// 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));**
Practice with arrays part 2
**// 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));**
Class Practice 3
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);
Class Practice 4
**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 to print arrays
int[] nums = {-5, 2, 23, -10}; // Arrays.toString() System.out.println(Arrays.toString(nums));