Reputation: 9
I would like to read a file which is in the form of a matrix, so i tried reading a file put it in String arraylist and then converted to integer array. Now I need a 2D integer array. Can anyone help? Is there any better way to do this.
public class readMat {
private static ArrayList<String> list = new ArrayList<String>();
public static void main (String[] args)
{
// read file and put in arraylist
try
{
Scanner s = new Scanner(new File("link_info_test.txt"));
while (s.hasNext())
{
list.add(s.next());
}
}
catch (Exception e)
{
e.printStackTrace();
}
String[] stockArr = new String[list.size()];
stockArr = list.toArray(stockArr);
int[] sum= Convert(stockArr);
}
// convert string arraylist to integer 1 dimensional array private static int[] Convert(String[] stockArr)
{
if (list != null)
{
int intarray[] = new int[stockArr.length];
for (int i = 0; i < stockArr.length; i++)
{
intarray[i] = Integer.parseInt(stockArr[i]);
}
return intarray;
}
return null;
}
}
Upvotes: 0
Views: 21657
Reputation: 19
Let's say you have temperature data for each day of the week for 10 weeks (that is 70 pieces of data). You want to convert it to a 2d array with rows representing weeks and columns representing days. Here you go:
int temp[70] = {45, 43, 54, ........}
int twoD[30][7]
for(int i=0; i < 70; i++) {
twoD[i / 7][i % 7] = temp[i]
}
That's it.
Upvotes: 2
Reputation: 71
If i understood, your question correctly you want to convert 1D array to 2D array ... You can use following method
public static int[][] convertArrayTo2DArray(final int[] _1darray) {
int[][] _2dArray = null;
int size = _1darray.length / 2;
if (_1darray.length % 2 == 0) {
_2dArray = new int[2][size];
} else {
_2dArray = new int[3][size];
}
int index = 0;
outter: for (int i = 0; i < _2dArray.length; i++) {
for (int j = 0; j < _2dArray[i].length; j++) {
if (index == _1darray.length) {
break outter;
}
_2dArray[i][j] = _1darray[index];
index++;
}
}
return _2dArray;
}
Upvotes: 0
Reputation: 369
Assuming that each entry of your String array consists of some integers, separated by some delimiter (comma, dot, hyphen, etc), then you can use the String.split() method. For instance, if your delimiter is a comma, then you would do something like this:
String Integer1;
String Integer2;
String[] TotalString;
TotalString = stockArr[i].Split(",");
Integer1 = TotalString[0];
Integer2 = TotalString[1];
Then just parse the Strings into integers and put them into your array.
Upvotes: 0
Reputation: 210
After the line
int[] sum= Convert(stockArr);
you have your entire file in a 1D array of integers. At this point, you have to determine the width and height of the 2D array.
Let's say you want the 2D array to have 3 rows and 4 columns as an example. Do this:
int[][] int_table = new int[3][4];
for(int j = 0; j < 3; j++)
{
for(int i = 0; i < 4; i++)
{
int_table[j][i] = sum[j * 4 + i];
}
}
The equation I'm using within sum's index is a conversion function that goes from 1D to 2D coordinates. Starting at both j and i being equal to 0, sum[j * 4 + i] = sum[0 * 4 + 0] = sum[0]. The variable i would increment by one at the next step, and we would have sum[0 * 4 + 1] = sum[1]. At the end of the row, i would reset to 0 and j would increment by 1. At that point, we would have sum[1 * 4 + 0] = sum[4], or sum's fifth element. This makes sense if you consider the first four elements as those of the first row. now that we're on a new row, we can fill it with the next four. The "four" that I've been mentioning is the width of the row that we defined earlier while declaring the 2D array.
Keep in mind that the 2D array's width and height can't multiply together to be larger than the total number of integers in the 1D array. You'll get an IndexOutOfBoundsException if you try to read beyond that size.
Upvotes: 1