Note that there are some explanatory texts on larger screens.

plurals
  1. POJava : Resizing a multidimensional array
    text
    copied!<p>I have a multidimensional array built from Strings that is initially created with the size [50][50], this is too big and now the array is full of null values, I am currently trying to remove these said null values, I have managed to resize the array to [requiredSize][50] but cannot shrink it any further, could anyone help me with this? I have scoured the internet for such an answer but cannot find it.</p> <p>Here is my complete code too (I realise there may be some very unclean parts in my code, I am yet to clean anything up)</p> <pre><code>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&lt;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)); } } </code></pre> <p>The CSV file looks as follows</p> <pre><code>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 </code></pre> <p>It can take any size obviously but this is just a sample file.</p> <p>I just need to resize the array so that it will not contain any null values.</p> <p>I also understand a list would be a better option here but it is not possible due to outside constraints. It can only be an multi dimensional array.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload