Hallo,
ich habe eine Programm erstellt, welches Häuser, die eine Höhe haben, nach dieser sortiert.
Dieses funktioniert auch, sofern ich die static modifier wegnehme. Ich möchte aber statische Methoden haben. Was habe ich falsch gemacht?
Main Klasse:
Ich bekomme nun folgenden Fehlercode:
./GenericQuickSort.java:4: non-static type variable T cannot be referenced from a static context
static T[] array;
^
./GenericQuickSort.java:11: non-static type variable T cannot be referenced from a static context
static T[] sort2(int unt,int ob){
^
./GenericQuickSort.java:21: non-static type variable T cannot be referenced from a static context
T hilfe=array;
ich habe eine Programm erstellt, welches Häuser, die eine Höhe haben, nach dieser sortiert.
Dieses funktioniert auch, sofern ich die static modifier wegnehme. Ich möchte aber statische Methoden haben. Was habe ich falsch gemacht?
Java:
public class GenericQuickSort <T extends Comparable<T>> {
static T[] array;
T[] sort(T[] array2){
this.array=array2;
return sort2(0,array.length-1);
}
static T[] sort2(int unt,int ob){
int x=(ob+unt)/2;
int i=unt;
int j=ob;
while(i<=j){
while(array[i].compareTo(array[x])<0) i++;
while(array[j].compareTo(array[x])>0) j--;
if(i<=j){
T hilfe=array[i];
array[i]=array[j];
array[j]=hilfe;
i++;
j--;
}
}
if(unt<j) sort2(unt,j);
if(i<ob) sort2(i,ob);
return array;
}
}
Main Klasse:
Java:
public class Main{
public static void main(String[] args){
Haus[] ferdi={new Haus(4),new Haus(8),new Haus(3),new Haus(4),new Haus(1),new Haus(3),new Haus(3)};
for(int i=0;i<ferdi.length;i++){
System.out.println(ferdi[i].toString());
}
GenericQuickSort<Haus> s;
s=new GenericQuickSort<Haus>();
ferdi=s.sort(ferdi);
for(int i=0;i<ferdi.length;i++){
System.out.println(ferdi[i].toString());
}
}
}
Ich bekomme nun folgenden Fehlercode:
./GenericQuickSort.java:4: non-static type variable T cannot be referenced from a static context
static T[] array;
^
./GenericQuickSort.java:11: non-static type variable T cannot be referenced from a static context
static T[] sort2(int unt,int ob){
^
./GenericQuickSort.java:21: non-static type variable T cannot be referenced from a static context
T hilfe=array;
Zuletzt bearbeitet: