Arrays
Results 1 to 5 of 5

Thread: Arrays

  1. #1
    Senior Member
    Join Date
    Dec 2001
    Posts
    134

    Arrays

    Hello Guys.
    I want to know how do we initialize the String array in java, when we do not know what will be the size of the array.
    like i want to store the following strings in the string array:
    ------------
    102dwd d fdw f fwf
    324 sdf sf sf sf s f
    dfe4 34 4 r32
    -------------------
    i am taking these lines from the txt file, and I do not know the size of the array.
    I have used: String Arr[];
    but the compiler says that i have to initialize the array, but i do not know the size.


    Any sort of information will be appreciated.
    Regards
    Harbir
    U get What U pay for.

  2. #2
    Banned
    Join Date
    Sep 2004
    Posts
    305
    I'd use an ArrayList like below:

    Code:
    import java.util.ArrayList;
    
    public class projectMain {
        
        public static void main(String[] args) {
            ArrayList arraylist = new ArrayList();
            
            String x = "hdfaldfadf";
            String y = "1556fadsfa";
            
            arraylist.add(x);
            arraylist.add(y);
            
            for(int a = 0; a < arraylist.size(); a++) {
                System.out.println(arraylist.get(a));
            }
            
            
        }
    }

  3. #3
    Senior Member
    Join Date
    Dec 2001
    Posts
    134
    String x = "hdfaldfadf";
    u r initializing the array, i can't do that, i do not know how many strings will come into the array.
    U get What U pay for.

  4. #4
    Banned
    Join Date
    Sep 2004
    Posts
    305
    Write to the string, add it to the ArrayList, then rewrite the string, and add to the ArrayList... repeat or just do arraylist.add("whatever");

  5. #5
    Member
    Join Date
    Oct 2004
    Posts
    40
    On the whole I think an ArrayList would be your best bet, there are some other alternatives, but all of them involve you essentially rewriting what is done in ArrayList, so just use what you've been given over writing it for yourself.
    --BigDick


    \"When in Rome, eat Rome!\" -Godzilla

Posting Permissions

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