• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

why the size of String array is dynamically increasing

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/* navneet is the directory name, as the size of the array is 0, than how it can print the list of all the file in the directory*/


import java.io.*;
class file2
{
public static void main(String aa[])
{
String[] s=new String[0];

File file=new File("navneet");
s=file.list();
System.out.println(s.length);
for(String s1:s)
System.out.println("found " + s1);

}

}
 
Ranch Hand
Posts: 47
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

navneet kumarbairwal wrote:
...
String[] s=new String[0];
...
s=file.list();
...



"s" is just reference to object of type String[], first you assign it to one element array of String objects (so s.length is 1), but then you changes its reference to point to ANOTHER array of Strings - those returned by file.list() (and s.length is different). Original arrays didn't change - you just changed the reference of "s" variable.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

small correction to the above answer. s.length = 0 first
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tomasz Sochanski wrote:"s" is just refference to object of type String[], first you assign it to one element array of String objects (so s.length is 1)


Actually it's a no element array and s.length is zero.

Edit: I'm a bit slow this morning
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Joanne

Redundancy.....
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can't modify a string object but you can change the string reference as Tomasz pointed out you have changed the reference of 's'.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joanne Neal wrote:Edit: I'm a bit slow this morning



Harsha is too
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strictly off the topic:
Yes, I am too. After all, both of us are best friends..lol
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As mentioned before, file.list() returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

Regards,
Dan
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic