1/32
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
what is an array?
aA Java array is a sequence of values, each of the same type
write a method called addTwo that takes in an int called val and adds 2 to it and prints it out
public void addTwo(int val){
val = val + 2;
System.out.println("After Add: " + val);
}
write a method called addTwo that takes in an array of integers and adds 2 to each element in the array
public void addTwo(int vals[]){
for(int i = 0; i < vals.length; i++){
vals[i] = vals[i] + 2;
}
}
load either an int, double or String into an Array in 1 line, load a variable called grades with 8,9,8,10 in 1 line
int grades[] = new int[]{8,9,8,10};
load a variable called food with the following values: Sushi, Pizza, and Burgers
String foods[] = new String[]{"Sushi","Pizza","Burgers"};
write the 4 lines of code to init an array called marks with values from 150 to 180
int marks[] = new int[31];
for(int i = 0; i < marks.length; i++){
marks[i] = 150 + i;
}
given an array of ints, return true if 6 appears as either the first or last element in the array. the array will be 1 length or more.
public boolean firstLast6(int[] nums){
}
public boolean firstLast6(int[] nums){
int num = nums.length;
if(num > 0){
if(nums[0] == 6){
return true;
} else if(nums[num - 1] == 6){
return true;
}
}
}
given an array of ints, return true if the array is length 1 or more, and the first element and the last element are equal.
public boolean sameFirstLast(int[] nums){
}
public boolean sameFirstLast(int[] nums){
int num = nums.length;
if(num >= 1 && nums[0] == nums[num - 1]){
return true;
}
return false;
}
return an int array length 3 containing the first 3 digits of pi, {3,1,4}.
public int[] makePi(){
}
public int[] makePi(){
int[] array = {3,1,4};
return array;
}
given 2 arrays of ints, a and b, return true if they have the same first element or they have the same last element. both arrays will be length 1 or more.
public boolean commonEnd(int[] a, int[] b){
}
public boolean commonEnd(int[] a, int[] b){
int aLen = a.length;
int bLen = b.length;
if(a[0] == b[0]){
return true;
} else if(a[aLen - 1] == b[bLen - 1]){
return true;
}
return false;
}
given an array of ints length 3, return the sum of all the elements.
public int sum3(int[] nums){
}
public int sum3(int[] nums){
int num = nums.length;
int sum = 0;
if(num >= 1){
sum += nums[0];
}
if(num >= 2){
sum += nums[1];
}
if(num >= 3){
sum += nums[2];
}
return sum;
}
given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
public int[] rotateLeft3(int[] nums){
}
public int[] rotateLeft3(int[] nums){
int num = nums.length;
int[] rotated = {nums[1], nums[2], nums[0]};
if(nums >= 3){
return rotated;
}
return rotated;
}
given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}.
public int[] reverse3(int[] nums){
}
public int[] reverse3(int[] nums){
int num = nums.length;
int[] reversed = {nums[2], nums[1], nums[0]};
if(num >= 3){
return reversed;
}
return reversed;
}
given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. return the changed array.
public int[] maxEnd3(int[] nums){
}
public int[] maxEnd3(int[] nums){
// Assume that the first element is the maximum
int max = nums[0];
if(nums[2] > max){
max = nums[2];
}
nums[0] = max;
nums[1] = max;
nums[3] = max;
}
given an array of ints, return the sum of the first 2 elements in the array. If the array length is less than 2, just sum up the elements that exist, returning 0 if the array is length 0.
public int sum2(int[] nums){
}
public int sum2(int[] nums){
int num = nums.length;
int val = 0;
int valTwo = 0;
if(num == 0){
return 0;
} else if(num == 1){
val = nums[0];
return val;
} else {
valTwo = nums[0] + nums[1];
return valTwo;
}
}
given 2 int arrays, a and b, each length 3, return a new array length 2 containing their middle elements.
public int[] middleWay(int[] a, int[] b){
}
public int[] middleWay(int[] a, int[] b){
int[] result = {a[1], b[1]};
return result;
}
given an array of ints, return a new array length 2 containing the first and last elements from the original array. The original array will be length 1 or more.
public int[] makeEnds(int[] nums){
}
public int[] makeEnds(int[] nums){
int val = nums.length - 1;
int[] result = {nums[0], nums[val]};
return result;
}
given an int array length 2, return true if it contains a 2 or a 3.
public boolean has23(int[] nums){
}
public boolean has23(int[] nums){
int num = nums[0];
int numTwo = nums[1];
if(num == 2 || num == 3){
return true;
} else if(numTwo == 2 || numTwo == 3){
return true;
}
return false;
}
given an int array length 2, return true if it does not contain a 2 or 3.
public boolean no23(int[] nums){
}
public boolean no23(int[] nums){
int num = nums[0];
int numTwo = nums[1];
if(num == 2 || num == 3){
return false;
} else if(numTwo == 2 || numTwo == 3){
return false;
}
return true;
}
given an int array, return a new array with double the length where its last element is the same as the original array, and all the other elements are 0. the original array will be length 1 or more. note: by default, a new int array contains all 0's.
public int[] makeLast(int[] nums){
}
public int[] makeLast(int[] nums){
int num = nums.length;
int[] result = new int[num * 2];
int res = result.length - 1;
if(num == 1){
result[res] = nums[0];
} else {
result[res] = nums[num - 1];
}
return result;
}
given an int array, return true if the array contains 2 twice, or 3 twice. The array will be length 0, 1, or 2.
public boolean double23(int[] nums){
}
public boolean double23(int[] nums){
int num = nums.length;
if(num == 2){
if(nums[0] == 2 && nums[1] == 2){
return true;
} else if(nums[0] == 3 && nums[1] == 3){
return true;
}
}
return false;
}
given an int array length 3, if there is a 2 in the array immediately followed by a 3, set the 3 element to 0. Return the changed array.
public int[] fix23(int[] nums){
}
public int[] fix23(int[] nums){
int first = nums[0];
int second = nums[1];
int third = nums[2];
if(first == 2 && second == 3){
nums[1] = 0;
} else if(second == 2 && third == 3){
nums[2] = 0;
}
return nums;
}
start with 2 int arrays, a and b, of any length. Return how many of the arrays have 1 as their first element.
public int start1(int[] a, int[] b){
}
public int start1(int[] a, int[] b){
int aLen = a.length;
int bLen = b.length;
int val = 0;
if(aLen >= 1 && a[0] == 1){
val += 1;
}
if(bLen >= 1 && b[0] == 1){
val += 1;
}
return val;
}
start with 2 int arrays, a and b, each length 2. consider the sum of the values in each array. Return the array which has the largest sum. in event of a tie, return a.
public int[] biggerTwo(int[] a, int[] b){
}
public int[] biggerTwo(int[] a, int[] b){
int sumA = a[0] + a[1];
int sumB = b[0] + b[1];
if(sumA >= sumB){
return a;
} else if(sumB >= sumA){
return b;
}
return a;
}
given an array of ints of even length, return a new array length 2 containing the middle two elements from the original array. the original array will be length 2 or more.
public int[] makeMiddle(int[] nums){
}
public int[] makeMiddle(int[] nums){
int num = nums.length;
int[] result = new int[2];
int mid = num / 2;
if(num % 2 == 0){
result[0] = nums[mid - 1];
result[1] = nums[mid];
}
return result;
}
given 2 int arrays, each length 2, return a new array length 4 containing all their elements.
public int[] plusTwo(int[] a, int[] b){
}
public int[] plusTwo(int[] a, int[] b){
int aLen = a.length;
int bLen = b.length;
int[] result = new int[4];
if(aLen == 2 && bLen == 2){
result[0] = a[0];
result[1] = a[1];
result[2] = b[0];
result[3] = b[1];
}
return result;
}
given an array of ints, swap the first and last elements in the array. Return the modified array. the array length will be at least 1.
public int[] swapEnds(int[] nums){
}
public int[] swapEnds(int[] nums){
int num = nums.length;
int val = nums[0];
if(num >= 1){
nums[0] = nums[num - 1];
nums[num - 1] = val;
}
return nums;
}
given an array of ints of odd length, return a new array length 3 containing the elements from the middle of the array. the array length will be at least 3.
public int[] midThree(int[] nums){
}
public int[] midThree(int[] nums){
int num = nums.length;
int mid = num / 2;
int[] result = new int[3];
if(num >= 3){
result[0] = nums[mid - 1];
result[1] = nums[mid];
result[2] = nums[mid + 1];
}
return result;
}
given an array of ints of odd length, look at the first, last, and middle values in the array and return the largest. The array length will be a least 1.
public int maxTriple(int[] nums){
}
public int maxTriple(int[] nums){
int num = nums.length;
int first = nums[0];
int middle = nums[num / 2];
int last = nums[num - 1];
if(first >= last && first >= middle){
return first;
} else if(last >= first && last >= middle){
return last;
} else {
return middle;
}
}
given an int array of any length, return a new array of its first 2 elements. If the array is smaller than length 2, use whatever elements are present.
public int[] frontPiece(int[] nums){
}
public int[] frontPiece(int[] nums){
int num = nums.length;
int[] val;
if(num >= 2){
val = new int[2];
val[0] = nums[0];
val[1] = nums[1];
} else if(num == 1){
val = new int[1];
val[0] = nums[0];
} else {
val = new int[0];
}
return val;
}
we'll say that a 1 immediately followed by a 3 in an array is an "unlucky" 1. return true if the given array contains an unlucky 1 in the first 2 or last 2 positions in the array.
public boolean unlucky1(int[] nums){
}
public boolean unlucky1(int[] nums){
int num = nums.length;
if(num < 2){
return false;
}
if(nums[0] == 1 && nums[1] == 3){
return true;
}
if(nums[1] == 1 && nums[2] == 3){
return true;
}
if(nums[num - 2] == 1 && nums[num - ] == 3){
return true;
}
return false;
}
given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. the arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.
public int[] make2(int[] a, int[] b){
}
public int[] make2(int[] a, int[] b){
int[] result = {0, 0};
int aLen = a.length;
if(aLen >= 2){
result[0] = a[0];
result[1] = a[1];
} else if(aLen == 1){
result[0] = a[0];
result[1] = b[0];
} else {
result[0] = b[0];
result[1] = b[1];
}
return result;
}
given 2 int arrays, a and b, of any length, return a new array with the first element of each array. If either array is length 0, ignore that array.
public int[] front11(int[] a, int[] b){
}
public int[] front11(int[] a, int[] b){
int[] result;
int aLen = a.length;
int bLen = b.length;
if(aLen >= 1){
result = new int[1];
result[0] = a[0];
if(bLen >= 1){
result = new int[2];
result[0] = a[0];
result[1] = b[0];
}
} else if(bLen >= 1){
result = new int[1];
result[0] = b[0];
} else {
result = new int[0];
}
return result;
}