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 3 of 3

Thread: Array of objects

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Array of objects

    i am writing a code for workers in inheritance .
    there is nothing wrong with code but i have a question..
    if i have 8 workers i will create an array of 8 objects:
    worker[] object = new worker[8];

    My question here ..
    if i don't know the number of workers what i will do ??

    and thanks in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Array of objects

    Use an ArrayList.
    If you must use arrays, create one of a reasonable size. When it gets full, create another larger array and copy the contents of the first array to the new one and set the array variable to refer to the new array.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    spark (August 4th, 2012)

  4. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array of objects

    Norm has it right on this one. You may and is highly recommended to use ArrayLists. I can not remember the last time I just used an array. ArrayLists can store vast amount of information and will double in size every time the AL is forced to allocate memory. It is also easier to sort and AL rather than an array.

Similar Threads

  1. Initializing an Array of Objects
    By Destro in forum Object Oriented Programming
    Replies: 1
    Last Post: April 20th, 2012, 07:25 AM
  2. Loop through a 2d array of objects
    By ssjg0ten5 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 28th, 2012, 10:53 PM
  3. Help with storing objects in array? :(
    By spoonemore11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 2nd, 2011, 05:28 PM
  4. all objects in array the same?
    By abrohm in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 17th, 2011, 12:21 PM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 02:38 PM