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.


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

Results 1 to 2 of 2

Thread: Defining an array

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Thanked 0 Times in 0 Posts

    Default Defining an array


    How to Definie an array that I do not know what size it will be?

    this is the array:

    Shape shapes[] = new Shape[];

    i dont know how many shapes will be entered,
    what am i suppose to write in the second brackets?

  2. #2
    Join Date
    Sep 2018
    My Mood
    Thanked 38 Times in 36 Posts

    Default Re: Defining an array

    In the second set of brackets you put the number of items to be contained in the array. If you need to add more items, you will need to reallocate the array and copy the elements. You can use the Arrays.copyOf() method to do this. However, I recommend using an instance of ArrayList() in lieu of an array since it does all of this book keeping for you.


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

    Yehonatan (October 2nd, 2018)

Similar Threads

  1. I need help defining a class.
    By blobman23 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 16th, 2013, 12:42 PM
  2. defining a constructor with a scanner
    By bombert in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2013, 07:55 AM
  3. help with defining a changing variable within a loop
    By uswhovian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 9th, 2013, 10:30 AM
  4. help with defining a method
    By mrjavajava in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 30th, 2013, 07:06 AM
  5. [SOLVED] What do I need to do to use a bean in Netbeans 7 apart from defining it?
    By Lord Voldemort in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: July 31st, 2011, 12:29 AM