• 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

Array vs java.util.ArrayList

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Recently i started learning Java with a nice book.
One of the examples uses io stuff in order to
read contents of a text file and stores it into an ArrayList instance.
Unfortunately, while this runs well on my box, i can't complie this example on another machine running MacOS X(10.2 - panther i think).
I guess, it's because the Mac is running Java 1.4.2 -
So here's my question :
Can i compile this on Windows and make it run on Mac ? (upgrading Java on MacOS X seems even more difficult than on debian )
Another solution i though of was replacing ArrayList with a simle String[] array, but how can i change the size of the array at run time ?

Thanks for your help
 
Julien Castelain
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so,far the only "solution" i found i this
(don't laugh too much, i a newbie )



but is this the "good" way to do it ?
 
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good start for a newbie .

Now let's see what we can improve in the code.

To start off with lets reduce the File I/O.

1. BufferedReader reader = new BufferedReader(new FileReader("test.txt"));
......
while((in = reader.readLine()) != null)

You are reading the file twice, first time to check the number of lines in the file and second time the actual content, the size of the array cannot be changed at runtime and hence I suppose you have gone for the above approach.

will it be ok for you if you read the lines into an arraylist and convert it into the string[], or do you "not" want to use collections at all and work only with the String [].

If reading into the arraylist and then converting into the string [] is ok with you, the API for collection classes provides you with the methods to return the content of the list in an [] format.

If you do not want to use any collection classes like ArrayList, Vector etc what you could do is while reading from the file you can keep on concating the content of the readline to a string but seperated by a token.
you can then use the tokenizer method to collect the differnt tokens into your String[].


2. Unless you have some reasons for storing each line in a different [] element, the better way to go about it is construct a single String for the whole file content.



PS: BufferedReader reader = new BufferedReader ...... that a good start for a newbie......
 
Julien Castelain
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for saying it's a good start (was this a joke ?)
actually the book i read was 600 pages,
so i guess i'm a bit less "newbier" than in october.

i really find ArrayList useful and i think i'll keep on reading about collections, but for this time i just want to use a String[]...

i think your approach i quite cool, so this is how i tried to manage it



it's nice that we only have to read the file once.
i also looked at the docs and it says String.concat "Concatenates the specified string to the end of this string" but it didn't work for me in the while loop above

i'm sure this can still be improved
thanks again
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by julien castelain:
thanks for saying it's a good start (was this a joke ?)

i'm sure this can still be improved
thanks again



No it was not a joke , I genuinely meant that.


i'm sure this can still be improved

"fileContents += in + sep;"

This line is your next target, it will create a lot of string objects coz of the immutable nature of String. Try to do a better implementation for the same.

Look into using StringBuffer in the place string "+" string

i also looked at the docs and it says String.concat "Concatenates the specified string to the end of this string" but it didn't work for me in the while loop above

Can you tell me in what way the concat method didn't work? we can try to work on that as well.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are immutable, so you can't actually change a string. If you read the full description of the concat method you will see

a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.


So you need to assign the return value of concat back to your original string.

You could also just do

Even better, if you are joining a lot of strings, is to use a StringBuffer or StringBuilder rather than a String.
 
Julien Castelain
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Devesh, Joanne,

sorry, if i'd been more patient (i would have seen
that the concat method works perfectly (like all
the stuff in the docs actually)...maybe that's another
thing i'll put on top of my "to do" list

the reason why i did

comes from my ActionScript programing experience...sorry for that one.

I think i'll look at StringBuilder (after work) and come up
with a new piece of code....

anyway, thanks a lot... you really helped me on this one
++
 
Julien Castelain
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i'm back
i used a StringBuilder instance this time

here's what has changed in my "test" code


but i guess this is the newbie way of using it.
another question now : why use StringBuilder rather than String ?
 
Devesh H Rao
Ranch Hand
Posts: 687
Hibernate jQuery Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by julien castelain:
ok i'm back


but i guess this is the newbie way of using it.
another question now : why use StringBuilder rather than String ?



I could'nt get my hands on StringBuilder source hence giving the explaination from a StringBuffer point of view, but I think the same will apply to StringBuilder too.


StringBuffer.append




String.concat



check the code marked in bold.... a string concat will always create a new string object which will result in a large number of String objects floating around when String class is used.

Normally a String object should be used for holding data, while the manipulations on the string should be done by using the helper classes provided like Stringbuilder, StringBuffer etc.
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic