Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Adding strings to an array dynamically, how can i do it?

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding strings to an array dynamically, how can i do it?

    im trying with arrayList now..
    Last edited by emkoirl; April 8th, 2011 at 02:14 PM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Adding strings to an array dynamically, how can i do it?

    You'll need to use an ArrayList realistically unless you know how many times the user is going to click
    ArrayList<String> myStringList = new ArrayList<String>();

    To add to an ArrayList, type myStringList.add("Hello World");
    To retrive from the ArrayList, for example, the first entry, you type myStringList.get(0);

    Click through these examples if you're still unclear/
    Getting an Element : ArrayListCollectionsJava Tutorial
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member anmaston's Avatar
    Join Date
    Apr 2011
    Location
    Plymouth, UK
    Posts
    10
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: Adding strings to an array dynamically, how can i do it?

    Hello buddy, I believe that your problem lies in the fact that you havent declared a size for the array.

        public static void main(String[] args) {
            String[] myArr = new String[20];
            int i = 0;
            Customer cust = new Customer();
            cust.setForename("Andrew");
            if (cust.getForename()=="Andrew") {
               currentString = cust.getForename();
                myArr[i] = currentString;
             }
        }

    That works and seems to be acheiving what you were after,

    Regards,

  4. #4
    Junior Member
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding strings to an array dynamically, how can i do it?

    edit: NVM i fixed it.. i was initialising the array everytime the user clicked a button so obviously it gave me an error

    thx for the help
    Last edited by emkoirl; April 8th, 2011 at 02:18 PM.

Similar Threads

  1. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 2
    Last Post: September 15th, 2011, 07:11 AM
  2. [SOLVED] adding to a multidimensional array.
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 27th, 2011, 10:09 AM
  3. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 08:48 PM
  4. dynamically resizing 2d array
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 2nd, 2010, 10:10 AM
  5. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 3
    Last Post: January 5th, 2010, 06:19 PM