Forum » Programiranje » Napaka pri sortirni metodi?
Napaka pri sortirni metodi?
ta_pravi ::
Lep pozdrav
Imam nalogo predelat sortirno metodo(mergesort), katera je prej delala na tabeli objektov tako da bo zdaj delala na vektorju objektov.
Javlja mi napako
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
pa nevem kaj bi lahko blo narobe...
//NAVADEN MERGE SORT KI DELA S TABELO
public static void mergesort(Element[] a)
{
msort(a,0,a.length);
}
public static void msort(Element[] a, int k, int n)
{
System.out.println("MSort("+k+","+n+")");
if (n>1)
{
msort(a,k,n/2);
msort(a,k+n/2,n-n/2);
merge(a,k,n);
a[0].izpisiTabelo(a);
}
}
public static void merge(Element[] a, int k, int n)
{
System.out.println("Merge("+k+","+n+")");
Element[] temp = new Element[n];
int i=0, lo=k, hi=k+n/2;
while(loMANJSEk+n/2 && hiMANJSEk+n)
temp[i++] = (a[lo].manjsi(a[hi]) ? a[lo++] : a[hi++]);
while(loMANJSEk+n/2)
temp[i++] = a[lo++];
while(hiMANJSEk+n)
temp[i++] = a[hi++];
for(i=0; iMANJSEn; i++)
a[k+i]=temp[i];
}
//MERGE SORT --- SORTIRANJE Z ZLIVANJEM (VEKTOR)
public static void mergesort(Vector|NepOglas| a)
{
msort(a,0,a.size());
}
public static void msort(Vector|NepOglas| a, int k, int n)
{
if (n>1)
{
msort(a,k,n/2);
msort(a,k+n/2,n-n/2);
merge(a,k,n);
}
}
public static void merge(Vector|NepOglas| a, int k, int n)
{
Vector|NepOglas| temp = new Vector|NepOglas|(n);
int i=0, lo=k, hi=k+n/2;
while(loMANJSEk+n/2 && hiMANJSEk+n)
temp.set(i++, (a.get(lo).compareTo(a.get(hi))MANJSE0 ? a.get(lo++) : a.get(hi++)));
while(loMANJSEk+n/2)
temp.set(i++, a.get(lo++));
while(hiMANJSEk+n)
temp.set(i++, a.get(hi++));
for(i=0; iMANJSEn; i++)
a.set(k+i, temp.get(i));
}
}
ve kdo kaj bi blo narobe??
Imam nalogo predelat sortirno metodo(mergesort), katera je prej delala na tabeli objektov tako da bo zdaj delala na vektorju objektov.
Javlja mi napako
java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0
pa nevem kaj bi lahko blo narobe...
//NAVADEN MERGE SORT KI DELA S TABELO
public static void mergesort(Element[] a)
{
msort(a,0,a.length);
}
public static void msort(Element[] a, int k, int n)
{
System.out.println("MSort("+k+","+n+")");
if (n>1)
{
msort(a,k,n/2);
msort(a,k+n/2,n-n/2);
merge(a,k,n);
a[0].izpisiTabelo(a);
}
}
public static void merge(Element[] a, int k, int n)
{
System.out.println("Merge("+k+","+n+")");
Element[] temp = new Element[n];
int i=0, lo=k, hi=k+n/2;
while(loMANJSEk+n/2 && hiMANJSEk+n)
temp[i++] = (a[lo].manjsi(a[hi]) ? a[lo++] : a[hi++]);
while(loMANJSEk+n/2)
temp[i++] = a[lo++];
while(hiMANJSEk+n)
temp[i++] = a[hi++];
for(i=0; iMANJSEn; i++)
a[k+i]=temp[i];
}
//MERGE SORT --- SORTIRANJE Z ZLIVANJEM (VEKTOR)
public static void mergesort(Vector|NepOglas| a)
{
msort(a,0,a.size());
}
public static void msort(Vector|NepOglas| a, int k, int n)
{
if (n>1)
{
msort(a,k,n/2);
msort(a,k+n/2,n-n/2);
merge(a,k,n);
}
}
public static void merge(Vector|NepOglas| a, int k, int n)
{
Vector|NepOglas| temp = new Vector|NepOglas|(n);
int i=0, lo=k, hi=k+n/2;
while(loMANJSEk+n/2 && hiMANJSEk+n)
temp.set(i++, (a.get(lo).compareTo(a.get(hi))MANJSE0 ? a.get(lo++) : a.get(hi++)));
while(loMANJSEk+n/2)
temp.set(i++, a.get(lo++));
while(hiMANJSEk+n)
temp.set(i++, a.get(hi++));
for(i=0; iMANJSEn; i++)
a.set(k+i, temp.get(i));
}
}
ve kdo kaj bi blo narobe??
ta_pravi
Gundolf ::
Lahko bi bilo, da poskušaš sortirat prazen vektor. Vsaj na to bi jaz pomislil, če bi mi javilo index out of bounds: 0
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | JavaOddelek: Programiranje | 1278 (646) | 123mg |
» | [java] Osnovna vprašanjaOddelek: Programiranje | 2636 (1643) | killa bee |
» | Program v COddelek: Programiranje | 1940 (1779) | darkkk |
» | c++ in linux/windowsOddelek: Programiranje | 1735 (1611) | rapvirus |
» | Vmesnik v JaviOddelek: Programiranje | 2276 (2059) | Camel |