Results 1 to 2 of 2
  1. #1
    vasavi.singh is offline Member
    Join Date
    Feb 2009
    Posts
    24
    Rep Power
    0

    Default Converting ArrayList to Array

    Hi,

    I am reading the data from a file and Storing in an array. The size of the file( in the sense no of lines) not known. So, for dynamic allocation of memory i used ArrayList.

    Can u tell me the way to return this ArrayList variable and how to catch it in calling function.

    I tried another method, storing all this data in an array and returning this array to calling function.

    I am putting the code how I did.

    import java.util.*;
    import java.io.*;

    public class ArrayCopying
    {
    public static void main(String [] args) throws IOException
    {
    String[] a=printOutputFile();
    // printing the array a
    for(int i=0;i<a.length;i++)
    System.out.println(a[i]);
    }
    public static String[] printOutputFile()
    {
    String arr[]=null;
    try
    {
    BufferedReader Source = new BufferedReader(new FileReader("PrintOutput.txt"));
    ArrayList Hits = new ArrayList();
    String line;
    while ((line = Source.readLine()) != null)
    {
    String tkn[] =line.split(",");
    for(int i=0;i<tkn.length;i++)
    Hits.add(tkn[i]);
    }
    arr=new String[Hits.size()];
    System.out.println(Hits.size()+" & "+arr.length); // Usage:checking purpose
    for (int i=0; i<Hits.size(); i++)
    System.out.println(Hits.get(i)); // Printing well
    for (int i=0; i<Hits.size(); i++)
    {
    System.out.println(Hits.get(i)); // reading the 1st string later it displaying null
    arr=(String [])Hits.toArray();
    System.out.println(arr[i]);
    }
    }
    catch(Exception e)
    {
    System.out.println("Exception: " + e);
    }
    return arr;
    }
    }

  2. #2
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    283
    Rep Power
    7

    Default

    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class ArrayCopying
    {
    public static void main(String [] args) throws IOException
    {
    String[] a=printOutputFile();
    // printing the array a
    for(int i=0;i<a.length;i++)
    System.out.println(a[i]);
    }
    public static String[] printOutputFile()
    {
    String arr[]=null;
    try
    {
    BufferedReader Source = new BufferedReader(new FileReader("PrintOutput.txt"));
    ArrayList Hits = new ArrayList();
    String line;
    while ((line = Source.readLine()) != null)
    {
    String tkn[] =line.split(",");
    for(int i=0;i<tkn.length;i++)
    Hits.add(tkn[i]);
    }
    arr=new String[Hits.size()];
    System.out.println(Hits.size()+" & "+arr.length); // Usage:checking purpose
    for (int i=0; i<Hits.size(); i++)
    System.out.println(Hits.get(i)); // Printing well
    //for (int i=0; i<Hits.size(); i++)
    //{
    //System.out.println(Hits.get(i)); // reading the 1st string later it displaying null
    arr=(String[])Hits.toArray(arr);
    
    
    //System.out.println(arr[i]);
    //}
    }
    catch(Exception e)
    {
    System.out.println("Exception: " + e);
    e.printStackTrace();
    }
    return arr;
    }
    }

Similar Threads

  1. Converting a String into a array.
    By taraxgoesxboom in forum New To Java
    Replies: 12
    Last Post: 02-22-2009, 06:29 AM
  2. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:36 PM
  3. Converting an Array to a Vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:44 PM
  4. Converting a Collection to an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:44 PM
  5. Converting ArrayList to Array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 11:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •