• 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

How to declare an array without specifying its size?

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am trying to declare a char array without specifying its size,
this is not working:
char[] myArray = new char[];
how to do it?
regards
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't be done. Arrays are immutable (in size, not in contents) and you have to specify a size. You either have to use java.util.ArrayList (which requires Object elements) or create your own dynamic array class.
 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joe Ess:
Can't be done. Arrays are immutable (in size, not in contents) and you have to specify a size. You either have to use java.util.ArrayList (which requires Object elements) or create your own dynamic array class.


Yes, you are right. Have to specify it. But I can use a loop to keep reading.
Thanks.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ArrayList was mentioned. This is a perfect fit if you don't know the size you'll need. For example, if you read a text file and make an array of lines you won't know how many lines there are until you're done. ArrayList will grow itself as you read and add more lines. If you really need an array, you can get an array from the ArrayList when you're done.
 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stan James:
ArrayList was mentioned. This is a perfect fit if you don't know the size you'll need. For example, if you read a text file and make an array of lines you won't know how many lines there are until you're done. ArrayList will grow itself as you read and add more lines. If you really need an array, you can get an array from the ArrayList when you're done.


Thanks, I have to read from an InputStream. So, I have to put
content into a byte buffer, then translate it into String or chars.
int byteNum = inputStream.read(buf);
String myString = new String(buf);
regards
[ December 18, 2003: Message edited by: rick collette ]
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rick collette:

Thanks, I have to read from an InputStream. So, I have to put
content into a byte buffer, then translate it into String or chars.
int byteNum = inputStream.read(buf);
String myString = new String(buf);
regards
[ December 18, 2003: Message edited by: rick collette ]



If you are reading lines of text, BufferedReader has a readLine() method which would allow you to dispense with the funk of byte arrays.
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, correct me if I;m wrong, but I though readLine() method is not reliable for reading line in as bytes. Maybe this was fixed.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adrian Yan:
Actually, correct me if I;m wrong, but I though readLine() method is not reliable for reading line in as bytes. Maybe this was fixed.


You are thinking of readLine() in
DataInputStream


public final String readLine()throws IOException
Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic