String Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amolbehl
    New Member
    • May 2007
    • 63

    String Array

    Code:
    public String[] gen,temp,backup;
    Now I need to declare them as array of unknown size. I tried

    Code:
    // Try 1
    this.gen    = new String[];
    // Try 2
    this.gen    = new String[' '];
    Nothing seems to work can anyone help me with this please.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by amolbehl
    Code:
    public String[] gen,temp,backup;
    Now I need to declare them as array of unknown size. I tried

    Code:
    // Try 1
    this.gen    = new String[];
    // Try 2
    this.gen    = new String[' '];
    Nothing seems to work can anyone help me with this please.

    Why not throw everything away and use ArrayLists instead?

    Comment

    • hsn
      New Member
      • Sep 2007
      • 237

      #3
      the last poster is correct.
      if you want to create an array without a size you cant do that with a normal array you have to use ArrayList or Vectors.
      with them you don't need to declare any size. you just add the element to the vector or to the arraylist.
      good luck

      Comment

      • bchaib
        New Member
        • Jan 2008
        • 20

        #4
        Originally posted by amolbehl
        Code:
        public String[] gen,temp,backup;
        Now I need to declare them as array of unknown size. I tried

        Code:
        // Try 1
        this.gen    = new String[];
        // Try 2
        this.gen    = new String[' '];
        Nothing seems to work can anyone help me with this please.
        If you don't know how many strings you are going to use don't use arrays but arraylists or other classes from collections ..
        When you make this:
        this.gen = new String[];
        you have to tell the memory how many adresses it has to allocate for you array
        for example

        this.gen = new String[453234];
        String[] gen; // declares an array of strings (strings = array of charachters)
        gen= new String[196868]; // allocates memory for 196868 strings

        gen[0] = new char{'a','b','c ','d'}; // initialize first element

        ...

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by bchaib
          gen[0] = new char{'a','b','c ','d'}; // initialize first element
          ...
          That of course doesn't even compile ...

          kind regards,

          Jos

          Comment

          • bchaib
            New Member
            • Jan 2008
            • 20

            #6
            Originally posted by JosAH
            That of course doesn't even compile ...

            kind regards,

            Jos
            That is absolutly not a Java implementation, but that's how it works ..

            Implementation you should give a string
            strVar[100] = some string

            thanks
            have a nice day

            Comment

            • amolbehl
              New Member
              • May 2007
              • 63

              #7
              Wow thanx a lot arrrayList thing worked great for me thanx a lot everyone.

              Comment

              Working...