class QI1 {
// method that prints an int array
public static void printIntArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
}//printArray
// method that inserts a new key at the index 0 of an array
public static void insertKey(int[] array, int key) {
array[0] = key;
}//insertValue
// method that removes and returns a key from the index 0 of an array
public static int removeKey(int[] array) {
int value = array[0];
for (int i = 1; i < array.length; i++) {
array[i - 1] = array[i];
}
array[array.length - 1] = 0;
return value;
}//insertValue
// method that copies values from one array to another array
// returns the new array after copying
public static String[] copyArray(String[] array) {
String[] copy = new String[array.length];
for (int i = 0; i < array.length; i++) {
copy[i] = array[i];
}
return copy;
}//insertValue
// method that checks the values in both argument arrays
// returns true if all values are equal
public static boolean arraysEqual(String[] array1, String[] array2) {
if (array1.length != array2.length) {
return false;
}
for (int i = 0; i < array1.length; i++) {
if (!array1[i].equals(array2[i])) {
return false;
}
}
return true;
}//arraysEqual
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6};
printIntArray(array);
insertKey(array, 0);
printIntArray(array);
System.out.println(removeKey(array));
printIntArray(array);
String[] colors = {"red", "green", "blue"};
String[] copyColors = copyArray(colors);
System.out.println(arraysEqual(colors, copyColors));
System.out.println(arraysEqual(colors, new String[]{"red", "blue", "green"}));
}//main
}//class
Comments
Leave a comment