Reputation: 3
i was trying to compare 2 variables in java but it gives me error and i cant figure it out.
I am reading matrix element then put it in temp then put that temp variable in an array. but it gives error when I try to put matrix element in temp and when I compare elements. error: array required, but float found. Anyone knows how to correct this ?
public float[] toSortedArray()
{
float b[];
float temp;
int index=0;
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
temp=a[m][n];
b[index++]=temp;
}
}
Arrays.sort(b);
System.out.print("[");
for(int z=0; z<(m*n)-1; z++)
{
System.out.print(b[z]+", ");
}
System.out.print(b[(m*n)-1]+"]\n");
}
Upvotes: 0
Views: 891
Reputation: 37023
There are several things needed in this:
Pass on the m, n parameter along with original 2D array like
public float[] toSortedArray(float[][] a, int m, int n)
define b array as
float b[] = new float[m*n];
In for loop (one within i and j var) (both loop should start with 0) use
temp=a[i][j];
instead of
temp=a[m][n];
At the end return b.
return b;
Upvotes: 2