1 |
/* opdracht1.java - Opdracht 1 of OOP2, generic typing.
|
2 |
Copyright 2015 Maarten Vangeneugden |
3 |
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
you may not use this file except in compliance with the License. |
6 |
You may obtain a copy of the License at |
7 |
|
8 |
https://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
10 |
Unless required by applicable law or agreed to in writing, software |
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
See the License for the specific language governing permissions and |
14 |
limitations under the License.*/ |
15 |
|
16 |
class opdracht1 { |
17 |
/** |
18 |
* Generic function that will swap two values, based on the given indexes. After this, it prints the entire value, in the new order. |
19 |
* @param array The array in which the swap needs to take place. |
20 |
* @param i The index of the value that will be swapped with the value in j. |
21 |
* @param j The index of the value that will be swapped with the value in i. |
22 |
* @pre The array is a list of types that needs 2 objects to be swapped with each other. |
23 |
* @post The array is equal to the given array, except for the values at the given indexes, which have been swapped. |
24 |
* @exception IndexOutOfBoundsException Thrown when one or more given indexes do not appear in the given array. |
25 |
* @exception NullPointerException The given array does not exist. |
26 |
* @return No actual return value, but this function will print the new order of the array to the console. |
27 |
*/ |
28 |
public static <T> void genericArraySwap(T[] array, int i, int j) { |
29 |
T tempValue = array[i]; // A temporary value is required to swap with another value. |
30 |
array[i] = array[j]; // Replacing i by j. |
31 |
array[j] = tempValue; // Replacing j by i |
32 |
|
33 |
for(T value : array) { // Looping through all values of the array. |
34 |
System.out.print(value + ", "); // Printing a value. |
35 |
} |
36 |
System.out.println(); // Simulating a RETURN. |
37 |
} |
38 |
} |
39 |
|