public static void main(String[] args) { int[] myIntArray = new int[25]; for (int i = 0; i < myIntArray.length; i++) { myIntArray[i] = i * 10; } printArray(myIntArray); }
public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.println("Element " + i + ", value is " + array[i]); } }
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) { int[] myIntegers = getIntegers(5); for (int i = 0; i < myIntegers.length; i++) { System.out.println("Element " + i + ", typed value was " + myIntegers[i]); } System.out.println("The average is " + getAverage(myIntegers)); }
public static int[] getIntegers(int number) { System.out.println("Enter " + number + " integer values.\r"); int[] values = new int[number];
for (int i = 0; i < values.length; i++) { values[i] = scanner.nextInt(); }
return values; }
public static double getAverage(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) { sum += array[i]; }
return (double) sum / (double) array.length; } }
Array 挑戰
題目
Create a program using arrays that sorts a list of integers in descending order.
Descending order is highest value to lowest.
In other words if the array had the values in it [106, 26, 81, 5, 15] your program should ultimately have an array with [106, 81, 26, 15, 5] in it.
Set up the program so that the numbers to sort are read in from the keyboard (Scanner).
Implement the following methods:
getIntegers has one parameter of type int which is the size of the array. It returns an array of entered integers from the keyboard.=
printArray accepts an array and prints out the contents of the array. It should be printed in the following format:
Element 0 contents 106
Element 1 contents 81
Element 2 contents 26
Element 3 contents 15
Element 4 contents 5
sortIntegers accepts the unsorted array. It should sort the array and return a new array containing the sorted numbers.
The scenario is:
getIntegers() is called.
The returned array from getIntegers() is then used to call sortIntegers().
The returned array from sortIntegers() is then printed to the console.
[Do not try and implement this. It is to give you an idea of how the methods will be used]
TIP: you will have to figure out how to copy the array elements from the passed array into a new array and sort them and return the new sorted array.
TIP: Instantiate (create) the Scanner object inside the method.
TIP: Be extremely careful about spaces in the printed message.
NOTE: All methods should be defined as public static NOT public.
NOTE: Do not add a main method to the solution code.
public static int[] getIntegers(int capacity) { System.out.println("Enter " + capacity + " integer values:\r"); int[] array = new int[capacity];
for (int i = 0; i < array.length; i++) { array[i] = scanner.nextInt(); }
return array; }
public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.println("Element " + i + " contents " + array[i]); } }
public static int[] sortIntegers(int[] array) { int[] sortedArray = new int[array.length]; for (int i = 0; i < array.length; i++) { sortedArray[i] = array[i]; }
boolean flag = true; int temp; while (flag) { flag = false; for (int i = 0; i < sortedArray.length - 1; i++) { if (sortedArray[i] < sortedArray[i + 1]) { temp = sortedArray[i]; sortedArray[i] = sortedArray[i + 1]; sortedArray[i + 1] = temp; flag = true; } } }
return sortedArray; } }
以下程式碼可以進行優化
1 2 3 4 5 6
int[] sortedArray = new int[array.length]; for (int i = 0; i < array.length; i++) { sortedArray[i] = array[i]; } // 可以用 Java 內建的語法優化 int[] sortedArray = Arrays.copyOf(array, array.length);
題目
Write a method called readInteger() that has no parameters and returns an int.
It needs to read in an integer from the user - this represents how many elements the user needs to enter.
Write another method called readElements() that has one parameter of type int
The method needs to read from the console until all the elements are entered, and then return an array containing the elements entered.
And finally, write a method called findMin() with one parameter of type int[]. The method needs to return the minimum value in the array.
The scenario is:
1. readInteger() is called.
2. The number returned by readInteger() is then used to call readElements().
3. The array returned from readElements() is used to call findMin().
4. findMin() returns the minimum number.
[Do not try and implement this. It is to give you an idea of how the methods will be used]
TIP: Assume that the user will only enter numbers, never letters.
TIP: Instantiate (create) the Scanner object inside the method. There are two scanner objects, one for each of the two methods that are reading in input from the user.
TIP: Be extremely careful about spaces in the printed message.
NOTE: All methods should be defined as private static.
NOTE: Do not add a main method to the solution code.
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int number = readInteger(); int[] array = readElements(number); int minNumber = findMin(array); System.out.println("The min number is " + minNumber); }
private static int readInteger() { System.out.println("Enter count:\r"); int number = scanner.nextInt(); scanner.nextLine(); return number; }
for (int i = 0; i < number; i++) { System.out.println("Please enter " + i + " element:\r"); values[i] = scanner.nextInt(); scanner.nextLine(); }
return values; }
private static int findMin(int[] array) { int minNumber = array[0];
for (int i = 0; i < array.length; i++) { if (array[i] < minNumber) { minNumber = array[i]; } }
return minNumber; } }
逆 Array 挑戰
題目
Write a method called reverse() with an int array as a parameter.
The method should not return any value. In other words, the method is allowed to modify the array parameter.
To reverse the array, you have to swap the elements, so that the first element is swapped with the last element and so on.
For example, if the array is [1, 2, 3, 4, 5], then the reversed array is [5, 4, 3, 2, 1].
The method should first print out the newly passed in array as Array = [1, 2, 3, 4, 5]
and then once it’s been reversed, print it out as Reversed array = [5, 4, 3, 2, 1]
TIP: When swapping the elements, use a variable to temporarily hold the current element.
NOTE: The method should be defined as private static.
NOTE: Do not add a main method to the solution code.
答案
修改 Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
public class ReverseArray { private static void reverse(int[] array) { System.out.println("Array = " + Arrays.toString(array));
int maxIndex = array.length - 1; int halfLength = array.length / 2; for (int i = 0; i < halfLength; i++) { int temp = array[i]; array[i] = array[maxIndex - i]; array[maxIndex - i] = temp; } System.out.println("Reversed array = " + Arrays.toString(array)); } }