154 lines
4.0 KiB
Java
154 lines
4.0 KiB
Java
package util;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
/**
|
|
* Write a description of class Util here.
|
|
*
|
|
* @author (your name)
|
|
* @version (a version number or a date)
|
|
*/
|
|
public class Util
|
|
{
|
|
public void printArray(int[] array)
|
|
{
|
|
for (int i = 0; i < array.length; ++i)
|
|
{
|
|
System.out.print(array[i]);
|
|
|
|
// Prüfen, ob wir nicht beim letzten Element sind.
|
|
// Auf das letzte Element sollte kein Komma folgen.
|
|
if (i < array.length - 1)
|
|
{
|
|
System.out.print(",");
|
|
}
|
|
}
|
|
|
|
// Nach Ausgabe brechen wir die Zeile um, damit folgende Ausgaben
|
|
// in einer neuen Zeile beginnen.
|
|
System.out.println();
|
|
}
|
|
|
|
public int randomInt(int upperLimit)
|
|
{
|
|
double random = Math.random(); // Zufallszahl zwischen 0.0 und 1.0 holen.
|
|
|
|
// Zufallszahl per Multiplikation auf 0.0-upperLimit skalieren
|
|
random = random * upperLimit;
|
|
|
|
// Nachkommastellen abschneiden und in Ganzzahlwert (int) umwandeln.
|
|
int result = (int) random;
|
|
|
|
return result;
|
|
}
|
|
|
|
public void fillArrayRandom(int[] array, int upperLimit)
|
|
{
|
|
for (int i = 0; i < array.length; ++i)
|
|
{
|
|
array[i] = randomInt(upperLimit);
|
|
}
|
|
}
|
|
|
|
public int[] concatArray(int[] array0, int[] array1)
|
|
{
|
|
int[] result = new int[array0.length + array1.length];
|
|
|
|
// Manuelles Kopieren mit Schleifen.
|
|
for (int i = 0; i < array0.length; ++i)
|
|
{
|
|
result[i] = array0[i];
|
|
}
|
|
for (int i = 0; i < array1.length; ++i)
|
|
{
|
|
// Um array0.length verschoben einfügen.
|
|
result[array0.length + i] = array1[i];
|
|
}
|
|
|
|
// Alternative Lösung unter Verwendung von System.arraycopy().
|
|
// System.arraycopy(array0, 0, result, 0, array0.length);
|
|
// System.arraycopy(array1, 0, result, array0.length, array1.length);
|
|
|
|
return result;
|
|
}
|
|
|
|
public int findMax(int[] array) {
|
|
int max = 0;
|
|
boolean found = false;
|
|
int loopRuns = 0;
|
|
for (int i = 0; !found && i < array.length; ++i) {
|
|
loopRuns++;
|
|
if (array[i] > max) {
|
|
max = array[i];
|
|
}
|
|
}
|
|
return loopRuns;
|
|
}
|
|
|
|
public int firstMatch(int[] array0, int[] array1) {
|
|
int loopRuns = 0;
|
|
for (int i = 0; i < array0.length; i++) {
|
|
for (int j = 0; j < array1.length; j++) {
|
|
loopRuns++;
|
|
if (array0[i] == array1[j]) {
|
|
return loopRuns;
|
|
}
|
|
}
|
|
}
|
|
return loopRuns;
|
|
}
|
|
|
|
public void fillArrayRandomRecursively(int[] array, int start, int upperLimit)
|
|
{
|
|
if (array.length - start <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
array[start] = randomInt(upperLimit);
|
|
fillArrayRandomRecursively(array, start + 1, upperLimit);
|
|
}
|
|
|
|
public void testRandomFillRecursively()
|
|
{
|
|
Util util = new Util();
|
|
int[] valueArray = new int[100];
|
|
util.fillArrayRandomRecursively(valueArray, 0, 100);
|
|
util.printArray(valueArray);
|
|
}
|
|
|
|
public int findMaxBySorting(int[] array){
|
|
Sorter sorter = new Sorter();
|
|
sorter.sort(array);
|
|
return array[array.length -1];
|
|
}
|
|
|
|
public Integer findValueBySorting(int @NotNull [] array, int value){
|
|
Sorter sorter = new Sorter();
|
|
sorter.sort(array);
|
|
int min = array[0];
|
|
int max = array[array.length -1];
|
|
if (value < min || value > max){
|
|
return null;
|
|
}
|
|
|
|
int low = 0;
|
|
int high = array.length - 1;
|
|
|
|
while (low <= high) {
|
|
int mid = (low + high) >>> 1;
|
|
if (array[mid] == value) {
|
|
return mid;
|
|
} else if (array[mid] < value) {
|
|
low = mid + 1;
|
|
} else {
|
|
high = mid - 1;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
|
|
}
|