Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Kristopher Davis   on Aug 02 In Java Category.

  
Question Answered By: Faith Hughes   on Aug 02

Every array  in Java is dinamically assigned at runtime,
this is why the definition of an array does not include
the number of elements. The number of the elements is
included in the initialization part.

So, you can do something like that:
double[] x = new double [10];

Or with an known number at compile time:
int n;
double[] x;
...you can read n from a file or user input here...
x = new double [n];

Or make another variable point to this variable, so
the both variables will share the same memory space.
double[] y = x;

This is not like C, where you define a number of
elements at compile time, and obviously this number
has to be constant. Well, in C++ you can create
arrays dynamically similar to Java.

But in Java, once you have assigned n entries for
your array you cannot change the number of elements,
as Nico said you can use a container class for that,
like ArrayList or Vector.

Share: 

 

This Question has 7 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on query for Java Exports Or get search suggestion and latest updates.


Tagged: