Arrays
popcorn hacks for arrays
String[] city_array = new String[4];
city_array[0] = "San Diego";
city_array[1] = "Los Angeles";
city_array[2] = "San Fransisco";
city_array[3] = "Sacramento";
System.out.println(Arrays.toString(city_array));
[San Diego, Los Angeles, San Fransisco, Sacramento]
city_array[1] = "Sacramento";
city_array[3] = "San Jose";
System.out.println(Arrays.toString(city_array));
System.out.println(city_array.length);
[San Diego, Sacramento, San Fransisco, San Jose]
4
int[] int_array = new int[2];
String[] String_array = new String[2];
double[] double_array = new double[2];
boolean[] boolean_array = new boolean[2];
System.out.println(int_array[1]);
System.out.println(String_array[1]);
System.out.println(double_array[1]);
System.out.println(boolean_array[1]);
0
null
0.0
false
Homework MCQ
-
D, to find the third to last index of an array you can use arr.length - 3, then the String method indexOf() can be used to find the index of the first occurance of the substring passed through, this this case the substring from the find argument
-
B, the arrays index of 1 is the value of 9, which the arrays index of 4 is the value of 6. (9)+(6)/2 = 12
6.2
int[] arr = {1,2,3,4,5};
public static int sumOfEvenNumbers(int[] array){
int total = 0;
for(int i = 0; i < array.length; i++){
if(array[i] % 2 == 0){
total += array[i];
}
}
return total;
}
System.out.println(sumOfEvenNumbers(arr));
6
public int countOccurences(int[] array, int target){
int count = 0;
for(int v: array){
if(v==target){
count += 1;
}
}
return count;
}
int[] arr = {3,4,5,6,3,2,1,3,3};
int tar = 3;
System.out.println(countOccurences(arr,tar));
4
public int firstNegativeIndex(int[] array){
int index = 0;
while(index < array.length && array[index] >= 0){
index += 1;
}
return index==arr.length?-1:index;
}
int[] arr = {1,2,-1,3};
System.out.println(firstNegativeIndex(arr));
3
public static int findSecondLargest(int[] arr) {
Arrays.sort(arr);
if(arr[arr.length - 1] == arr[arr.length - 2]){
return -1;
}
return arr[arr.length - 2];
}
// Example usage:
public static void main(String[] args) {
int[] arr1 = {3, 1, 4, 1, 5, 9, 2, 6};
System.out.println(findSecondLargest(arr1)); // Output: 6
int[] arr2 = {10, 10, 10, 10};
System.out.println(findSecondLargest(arr2)); // Output: -1
}
main(null);
6
-1
6.3
String[] languages= {"Java","Python","C#","Javascript"};
for(String v: languages){ //simplified it to a for each loop instead of a index loop
System.out.println(v);
}
Java
Python
C#
Javascript
String[] myArray = {"Hello","World,","I'm","An","Array!"};
// Add a loop to go through the array here
for (String value : myArray){
System.out.print(value + " ");
}
Hello World, I'm An Array!
Multiple Choice: of the options only option D would not result in an error, so I will assume that is the one you want me to choose
String[] fruits = {"Apple", "Banana", "Orange"};
for (String fruit: fruits) {
System.out.println("Word of "+ fruit);
char[] fruitChars = fruit.toCharArray();
for(int i = 0; i < fruitChars.length; i++){
if(Character.toLowerCase(fruitChars[i]) == 'a'){
System.out.println(i);
}
}
};
Word of Apple
0
Word of Banana
1
3
5
Word of Orange
2
String[] myArray = {"Object 1", "Object 2", "Object 3", "Object 4", "Object 5"};
for (currentWord: myArray) {
System.out(currentWord)
};
- missing dataType at “for (currentWord”
- Should be System.out.print (ususally println);
- missing a semi-colon on the same line as the previous error
Integer[] grades = {88, 93, 55, 68, 77};
Scanner userGrades = new Scanner(System.in);
System.out.println("Enter a grade: ");
int grade = Integer.parseInt(userGrades.nextLine());
System.out.print("average: ");
int total = grade;
for(Integer value: grades){
total += value;
}
System.out.println((double)total/((double)(grades.length + 1)));
grades = Arrays.copyOf(grades, grades.length + 1);
grades[grades.length - 1] = grade;
System.out.println(Arrays.toString(grades));
Enter a grade:
average: 64.66666666666667
[88, 93, 55, 68, 77, 7]
6.4
public class MaxMinInArray {
public static void findMaxAndMin(int[] array) {
if (array == null || array.length == 0) {
System.out.println("Array is empty");
return;
}
int max = array[0];
int min = array[0];
for (int i = 1; i < array.length; i++) {
// Add code here
if (array[i] > max) {
max = array[i];
}
if (array[i] < min) {
min = array[i];
}
if(array[i]>array[i-1]){
System.out.println("Increasing");
}
else {
System.out.println("Decreasing");
}
}
System.out.println("Maximum value: " + max);
System.out.println("Minimum value: " + min);
}
public static void main(String[] args) {
int[] array = {3, 5, 7, 2, 8, -1, 4, 0, 12};
findMaxAndMin(array);
}
}
MaxMinInArray.main(null)
Increasing
Increasing
Decreasing
Increasing
Decreasing
Increasing
Decreasing
Increasing
Maximum value: 12
Minimum value: -1
Integer[] myArray = {0, 1, 2, 3, 4, 5};
for (int i=myArray.length-1; i >= 0; i-=1) {
System.out.println(myArray[i]);
};
5
4
3
2
1
0
public class WordShifter {
public static void main(String[] args) {
String[] words = {"alpha", "beta", "gamma", "delta"};
int shiftWord = 2;
for (int count = 0; count < shiftWord; count++) {
String temp = words[0];
for (int index = 0; index < words.length - 1; index++) {
words[index] = words[index + 1];
}
words[words.length - 1] = temp;
}
for (int i = 0; i < words.length; i+=2) { //print out words, skipping every other
System.out.print(words[i] + " ");
}
}
}
WordShifter.main(null)
gamma alpha