I have 2D Array (DataValueForStation
) that represent id station in the first column and value of the station in the second column. Sometimes the id Station are not in normal order. There are also 2D array of AtStation
that consist ID Station but in random order. I don't know how to create 1d array for AtStation
and that's why i create only 2D array. The problem is I want to create an array (ValueStation
) that extract the value of the station by using Id Station as a identifier. The error shows ArrayIndexOutofBoundException
.
import java.util.Arrays;
public class Looping {
public static void main(String[] args)
{
double [][] DataValueForStation = {{11, 1000}, {22, 2000}, {35, 3000}, {46,4000}};
int [][] AtStation = {{22},{46},{35},{11}};
double [][] ValueStation = new double [AtStation.length][0];
int j = 0;
for (int i=0; i<DataValueForStation.length; i++)
{
if (j<AtStation.length && AtStation[j][0] == DataValueForStation[i][0])
{
ValueStation[j][0] = DataValueForStation[i][1];
j++;
}
else if (j<AtStation.length && AtStation[j][0] != DataValueForStation[i][0])
{
for (int k=0; k<DataValueForStation.length; k++)
{
if (j<AtStation.length && AtStation[j][0] == DataValueForStation[k][0])
{
ValueStation[j][0] = DataValueForStation[k][1];
j++;
}
}
}
}
System.out.println(Arrays.deepToString(ValueStation));
}
}
Update the below line
double [][] ValueStation = new double [AtStation.length][0];
to
double [][] ValueStation = new double [AtStation.length][1];
That specifies, you will have one element in the internal array.