Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array.length <?>

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
US
thanks for the help in advance,

i have the following

int n[] = new int[1500];

and i am wondering what is the maximum length for a given array. in my case, if i put 150000 it fails to compile. any ideas?

thanks
 
Are you sure ?
I can compile this fine :

int [] i = new int[1500000000];

but when I execute it, the JVM runs out of memory trying to reserve that amount of memory (with the default memory assigned - if you assign more via -Xmx it will be OK).

This however will not compile :

int [] i = new int[15000000000];

becuase you must constuct a primitive array with an int - and that number is too large for an int.

The largest int you can use is Integer.MAX_VALUE - which is 2147483647 .

--------------------------------------------------
Free Database Connection Pooling Software
 
o ok, my problem is with 150000 which falls into 32bit. for some reason, i cannot compile


int [] i = new int[150000];
 
this is the code i have.

import javax.swing.*;

public class arrayTest
{

public static void main (String args[])
{
int n[];
n = new int[150000];
String output = "";

for (int i=0; i<n.length; i++)
n = 1+2*i;


output += "subscript\tvalue\n";

for(int i=0; i<n.length; i++)
output += i +"\t" + n +"\n";

JTextArea outputArea = new JTextArea(30,10);
outputArea.setText(output);

JOptionPane.showMessageDialog (null, outputArea, "array info", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}
}


the showMessageDialog does not pop up to show the results...
 
So your first post - when you said "does not compile" - is not actually the case is it ?

There is nothing *technically* wrong with your code. Why you *think* it does not work is because you using up the RAM assigned to the JVM (creating this huge String object), murdering the CPU and the whole process is slowing down because the 'for' loop is killing the stack.

Run the code below which shows you visually what is happening to your machine :

Code:
import javax.swing.*;

public class arrayTest
{

   public static void main (String args[])
   {
      int n[];
      n = new int[150000];
      String output = "";

      for (int i=0; i<n.length; i++)
        n[i] = 1+2*i;


      output += "subscript\tvalue\n";
		System.out.println("starting loop ...");
      for(int i=0; i<n.length; i++) {
        output += i +"\t" + n[i] +"\n";
        System.out.print(i +"(" +output.length() +") ");
	  }
      System.out.println("done.");

      JTextArea outputArea = new JTextArea(30,10);
      outputArea.setText(output);

      JOptionPane.showMessageDialog (null, outputArea, "array info", JOptionPane.INFORMATION_MESSAGE);

      System.exit(0);
  }
}

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top