• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays - fill( ) method

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is written everywhere in K&B that array can't be modified . but in 1.4 APIs itself I am seeing fill() method .

please solve my doubt .
thanks .
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rathi ji:
It is written everywhere in K&B that array can't be modified .



Where is that statement ? ( Page No ?) In what context ?
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean to say : the differece b/w array & array list is that array list are modifiable array .
It is written on 426 page , 12th line : Think of this as a growable array ( in defination of array list )

please solve my doubt . if it is not true than please can you provide me little code . please ...
thanks .
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In both Arrays and ArrayList we can modify the values. ArrayList are growable meaning when you create the ArrayList with specified initial capacity ( by using it's constructor ), we can still add more elements than the capacity. The ArrayList will just grow to accomodate those extra elements.
So, if you create ArrayList with initial capacity of say 2 elements, it is possible to add more than 2 elements as shown in the code below.



As far as Arrays are concerned, I think you cannot add more than the number of elements you specify at the creation of array.
for eg:
int i[] = new int[2];
i[0] = 1; //OK
i[1] = 2; //OK
i[2] = 3; //Exception and cannot grow the int array.

I hope this is difference you were looking for between Arrays and ArrayList.

Let me know if you disagree on anything of this. May be I will learn more too
[ January 31, 2005: Message edited by: Jay Pawar ]
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as rathi ji says arrays are immmutable so the variable it refers to(in a 1d array) should not change but there`s a method called fill which changes the values of these variables
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really sorry , actually I was thinking fill() method like it can modify the size of array but it is just to insert elements in array .
Is everything right in this ?
thank you very much .
 
reply
    Bookmark Topic Watch Topic
  • New Topic