title: How to convert a one-dimensional array to two-dimensional array in Java
body: <p>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.</p>
  
  <pre><code>public class readMat {
  
  private static ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
    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 &lt; stockArr.length; i++) 
             {
             intarray[i] = Integer.parseInt(stockArr[i]);
             }
             return intarray;
             }
             return null;
           }
  
       }
  </code></pre>