Discussion on:

7
Comments

Join the conversation!

Follow via:
RSS
Email Alert
0 Votes
+ -
how to allocate memory for an array which have not size during the intialization.for ex i have used the code like this,but this shows NullPointer Exception

StringBuffer[] a;

public void _a(){
for( int i = 0 ; i 10; i++) a = new StringBuffer();
}
what possibly a problem is and how to allocate memory for the arrays during runtime
0 Votes
+ -
piopio 13th Nov 2002
your code misses the attribution of sufficient memory for the array. Before creating any of the StringBuffers, you have to create the "a" array.

Here is one possible answer:

StringBuffer[] a;

public void _a(){
int size=10; // size wasunknown before
a = new StringBuffer[size];
for (int i=0; ia.length; i++)
a = new StringBuffer();
}
The line
StringBuffer[] a;

does nothing more than declare the variable 'a' as a reference holder to a StringBuffer[] object. Yes, in Java, arrays are actually objects. So you have to construct a new array object before you start setting its members. The simplest way is to just do this:
StringBuffer[] a = new StringBuffer[10];

This creates a new array object that can hold 10 StringBuffer references. This doesn't construct the StringBuffers. You still have to do that as you were doing in the code fragment posted in your question.

The size of the array does not have to be a constant defined at compile time. It can be a runtime value, perhaps based on user input or on command line arguments. However it is derived, you can use a runtime value to specify the array size the same way you might use a constant (like 10).

Best regards,
Jim Cakalic
public static byte[] read2arrayFast(String file) throws Exception, java.io.IOException {
// File size " + Integer.MAX_VALUE);
}

byte[] out = new byte[(int)fileSize];

InputStream in = new BufferedInputStream(new FileInputStream(objFile));

try {
int readFileSize = in.read(out, 0, (int)fileSize);
} finally {
if (in != null) try{ in.close();}catch (Exception e){}
}
return out;
}
What happened in the second case when the file size in greater that 5 GB.?

You are calculating the len(integer) variable and create a final buffer (byte).
Integer support 2^31-1 ..so how can u staore more than this in a integer variable..
0 Votes
+ -
i think this is good way to efficient reading..
but is there a good way to write large files (more than 10 m.b.) efficiently???
Cannot access BigFileReader.java. Can you please check it? Thanks!
Keyboard Shortcuts:
Prev
Next
Toggle
Join the conversation
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.