Java: resizing a multidimensional array

I have a multidimensional array constructed from strings, originally created with a size of [50] [50], this is too large, and now the array is filled with zero values. I'm currently trying to remove these specified values, I managed to resize the array to [requiredSize] [50], but cannot reduce it, can anyone help me with this? I tried the Internet for such an answer, but I can not find it.

Here is my complete code too (I understand that there may be some very unclean parts in my code, I still need to remove something)

import java.io.*; import java.util.*; public class FooBar { public static String[][] loadCSV() { FileInputStream inStream; InputStreamReader inFile; BufferedReader br; String line; int lineNum, tokNum, ii, jj; String [][] CSV, TempArray, TempArray2; lineNum = tokNum = ii = jj = 0; TempArray = new String[50][50]; try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Please enter the file path of the CSV"); String fileName = in.readLine(); inStream = new FileInputStream(fileName); inFile = new InputStreamReader(inStream); br = new BufferedReader(inFile); StringTokenizer tok,tok2; lineNum = 0; line = br.readLine(); tokNum = 0; tok = new StringTokenizer(line, ","); while( tok.hasMoreTokens()) { TempArray[tokNum][0] = tok.nextToken(); tokNum++; } tokNum = 0; lineNum++; while( line != null) { line = br.readLine(); if (line != null) { tokNum = 0; tok2 = new StringTokenizer(line, ","); while(tok2.hasMoreTokens()) { TempArray[tokNum][lineNum] = tok2.nextToken(); tokNum++; } } lineNum++; } } catch(IOException e) { System.out.println("Error file may not be accessible, check the path and try again"); } CSV = new String[tokNum][50]; for (ii=0; ii<tokNum-1 ;ii++) { System.arraycopy(TempArray[ii],0,CSV[ii],0,TempArray[ii].length); } return CSV; } public static void main (String args[]) { String [][] CSV; CSV = loadCSV(); System.out.println(Arrays.deepToString(CSV)); } } 

The CSV file is as follows

 Height,Weight,Age,TER,Salary 163.9,46.8,37,72.6,53010.68 191.3,91.4,32,92.2,66068.51 166.5,51.1,27,77.6,42724.34 156.3,55.7,21,81.1,50531.91 

It can take any size, but this is just a sample file.

I just need to resize the array so that it does not contain any null values.

I also understand that a list would be the best option here, but this is not possible due to external constraints. It can only be a multidimensional array.

+4
source share
3 answers

I think you need 3 changes in your program

  • After the while lineNum while is 1 more than the number of lines in the file, so instead of declaring CSV to String[tokNum][50] declare it as CSV = new String[tokNum][lineNum-1];
  • tokNum will be the number of fields in the row, so your loop condition should be ii<tokNum , not ii<tokNum-1
  • The last parameter for arraycopy should be lineNum-1

i.e. modified code to create your CSV array:

 CSV = new String[tokNum][lineNum-1]; for (ii=0; ii<tokNum ;ii++) { System.arraycopy(TempArray[ii],0,CSV[ii],0,lineNum-1); } 

and the output will be as follows:

 [[Height, 163.9, 191.3, 166.5, 156.3], [Weight, 46.8, 91.4, 51.1, 55.7], [Age, 37, 32, 27, 21], [TER, 72.6, 92.2, 77.6, 81.1], [Salary, 53010.68, 66068.51, 42724.34, 50531.91]] 

Note that you really don't need to process the first line of the file separately from the others, but this is what you can cover as part of your cleanup.

+2
source

10 to 1 is homework. However, it looks like you put something into it.

Do not modify the TempArray variable. Create a List of Row Lists. Sort of:

 List<List<String>> rows = new ArrayList<ArrayList<String>>(); while(file.hasMoreRows()) { //not valid syntax...but you get the jist String rowIText = file.nextRow(); //not valid syntax...but you get the jist List<String> rowI = new ArrayList<String>(); //parse rowIText to build rowI --> this is your homework rows.add(rowI); } //now build String[][] using fully constructed rows variable 
+2
source

Here is the observation and suggestion.

Observation. Working with (multi-dimensional) arrays in Java is complicated.

Suggestion: Do not use arrays to represent complex data types in Java.

Create classes for your data. Create a list of people:

 class Person { String height; //should eventually be changed to a double probably String weight; // " //... public Person( String height, String weight /*, ... */ ) { this.height = height; this.weight = weight; //... } } List<Person> people = new ArrayList<Person>(); String line; while ( (line = reader.nextLine()) != null ) { String[] records = line.split(","); people.add(new Person (records[0], records[1] /*, ... */)); } 
0
source

All Articles