Methoden auf Arrays aller primitiver Typen zusammenfassen?

  • Themenstarter Themenstarter Guest
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
G

Guest

Gast
Moinsen!
Ich würde gerne einige Methoden schreiben, die auf Arrays aller (numerischer) primitiver Datentypen arbeiten, ohne diese für jeden primitiven Datentyp einzeln implementieren zu müssen.

Beispiel: Um das Maximum in einem Array zu bestimmen würde ich derzeit schreiben:
Code:
public static int getMax(int[] arr) {
	int max = arr[0];
	for(int i=1; i<arr.length; i++) {
		if (arr[i] > max)
			max = arr[i];
	}
	return max;
}

public static float getMax(float[] arr) {
	float max = arr[0];
	for(int i=1; i<arr.length; i++) {
		if (arr[i] > max)
			max = arr[i];
	}
	return max;
}

// ebenso für long, double, short, ...

Kann man das nicht irgendwie zusammenfassen ? Z.B. zu sowas in der Art wie (nicht lauffähiger code):
Code:
public static <T> getMax(<T>[] arr) {
	T max = arr[0];
	for(int i=1; i<arr.length; i++) {
		if (arr[i] > max)
			max = arr[i];
	}
	return max;
}

...
{
	// Aufrufe z.B. per:

	int[] A;
	...
	int m = getMax(A);

	float[] B;
	...
	float f = getMax(B);

}
Ich habe da nun etwas mit Reflections herumprobiert, aber nichts brauchbares gefunden.
Einmal scheiterte es lediglich daran, dass man nicht von z.B. int[] nach Integer[] oder Object[] casten kann.

Hat da wer eine Idee oder ein Patentrezept ?
 
Ist doch ganz einfach.😉
Vielleicht mal Suns Generics-Tutorial lesen...
Code:
public class Test<T extends Number>
{
  public T getMax(T[] arr) { 
    T max = arr[0]; 
    for(int i=1; i<arr.length; i++) { 
       if (arr[i].longValue() > max.longValue()) 
          max = arr[i]; 
    } 
    return max; 
  }

  public static void main(String... args)
  {
    Test<Integer> t = new Test<Integer>();
    Integer[] ints = {1,2,3,4};
    System.out.println(t.getMax(ints));
   
    
    
  }
 
}
 
Das löst das Problem aber nicht wirklich, da zwar von int nach Integer und damit auch nach Number gecastet wird, aber nicht von int[] nach Integer[] und damit auch nicht nach Number[].

Am einfachsten wäre wohl, im ganzen Programm die Bufferclasses zu verwenden. Bei der Verwendung von für primitive Datentypen gedachten Operatoren (+, -, /, *) sollte das keine Probleme geben, da automatisch gecastet wird, sicher bin ich mir aber nicht.
 
Man kann doch einfach ne kleine Helper-Funktion schreiben, die X-Arrays in Y-Arrays umwandelt. Also halt ein neues Array des passenden Typs erstellt. Dann überlädt man getMax() und ruft erst die Helper-Funktion auf.
Das ist natürlich alles eher unelegant...Aber da führt in Java wohl kein weg dran vorbei.
 
@Kim
So ähnlich habe ich es bereits gehabt, aber das klappt nicht mit primitiven Datentypen.
Z.B. geht weder:
Code:
Test<Integer> t = new Test<Integer>();
int[] ints = {1,2,3,4};
System.out.println(t.getMax(ints));
noch
Code:
Test<int> t = new Test<int>();
int[] ints = {1,2,3,4};
System.out.println(t.getMax(ints));

Das ist natürlich alles eher unelegant...Aber da führt in Java wohl kein weg dran vorbei.
Yo, das wäre ganz schöne Platzverschwendung ;-)

@Gast
Mit den 'Bufferclasses', bzw. 'Wrapperklassen', bspw. 'Integer' statt 'int', etc. gibt es mit den Operatoren wirklich kein Problem, da z.B.
Code:
Integer c = a + b;
ohne weiteres funktioniert.

Allerdings bedeutete das bei großen Arrays Geschwindigkeitseinbußen und Platzverschwendung.
Siehe dazu hier: http://www.java-forum.org/de/viewtopic.php?p=334885#334885

Zudem möchte ich meine Funktionen als Bibliothek in Projekten nutzen, in denen aus diesen Performancegründen Arrays primitiver Datentypen genutzt werden.

Hat noch wer eine Idee ?
 
es geht einfach nicht, schau dir z.B. die Arrays-Klasse an

static int binarySearch(byte[] a, byte key)
Searches the specified array of bytes for the specified value using the binary search algorithm.

static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)
Searches a range of the specified array of bytes for the specified value using the binary search algorithm.

static int binarySearch(char[] a, char key)
Searches the specified array of chars for the specified value using the binary search algorithm.

static int binarySearch(char[] a, int fromIndex, int toIndex, char key)
Searches a range of the specified array of chars for the specified value using the binary search algorithm.

static int binarySearch(double[] a, double key)
Searches the specified array of doubles for the specified value using the binary search algorithm.

static int binarySearch(double[] a, int fromIndex, int toIndex, double key)
Searches a range of the specified array of doubles for the specified value using the binary search algorithm.

static int binarySearch(float[] a, float key)
Searches the specified array of floats for the specified value using the binary search algorithm.

static int binarySearch(float[] a, int fromIndex, int toIndex, float key)
Searches a range of the specified array of floats for the specified value using the binary search algorithm.

static int binarySearch(int[] a, int key)
Searches the specified array of ints for the specified value using the binary search algorithm.

static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
Searches a range of the specified array of ints for the specified value using the binary search algorithm.

static int binarySearch(long[] a, int fromIndex, int toIndex, long key)
Searches a range of the specified array of longs for the specified value using the binary search algorithm.

static int binarySearch(long[] a, long key)
Searches the specified array of longs for the specified value using the binary search algorithm.

static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)
Searches a range of the specified array for the specified object using the binary search algorithm.

static int binarySearch(Object[] a, Object key)
Searches the specified array for the specified object using the binary search algorithm.

static int binarySearch(short[] a, int fromIndex, int toIndex, short key)
Searches a range of the specified array of shorts for the specified value using the binary search algorithm.

static int binarySearch(short[] a, short key)
Searches the specified array of shorts for the specified value using the binary search algorithm.

static <T> int
binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
Searches a range of the specified array for the specified object using the binary search algorithm.

static <T> int
binarySearch(T[] a, T key, Comparator<? super T> c)
Searches the specified array for the specified object using the binary search algorithm.
 
Ich hoffte, dass die häufige Überladung dieser Methoden noch ein Relikt aus 1.4.* und ggf. zur Abwärtskompatibilität vorhanden war, zu dem seit Reflections eine Alternative existiert. Noch gebe ich die Hoffnung nicht auf...

Bevor ich dann schließlich alles doch per Hand dupliziere schreibe ich mir dann aber lieber einen Mini-Präprozessor, welcher durch simple Textersetzungen und -wiederholungen aus einer Template-Methode entsprechende Methoden der Java-Klasse generiert.
 
mhm... du machst ein wesentlich komplexeres und aufwaenderigeres System nur weil du zu faul bist n bisschen copy & paste zu machen ?

naja
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben