# Array problem

• 02-13-2010, 07:07 PM
binarzt
Array problem
Hey people,

I searched through alot of forums, but I didn't find a clue of what do.

I have this short program here, but I always get this message:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at A6c.polynom(A6c.java:18)
atA6c.main(A6c.java:7)

Here is the code:

Code:

```class A6c {   public static void main ( String[] Argument)   {     double [] a;         double [] c = {2,3,4,5,6,7};         a = polynom(c);         for (int i = 0; i < a.length;i++)         System.out.println(a[i]);   }       public static double [] polynom (double [] a)   {     double [] b = {0};         for (int i = 0; i < a.length;i++)         {           b[i] = i*a[i];         }         return b;   }  }```

I hope you can help us. We are really desperate now, because we need it for a project, but we just don't get it right.

Greetz

• 02-13-2010, 07:15 PM
hardwired
Code:

```    public static double[] polynom(double[] a)     {         double[] b = //{0};                     new double[a.length];```
• 02-13-2010, 07:24 PM
FON
You got:
java.lang.ArrayIndexOutOfBoundsException: 1

because in runtime in second iteration of your loop happens:

b[1] = 1*a[1];

That element does not exist because you defined your array b :

double [] b = {0};

that means you have only one element in array.

double [] b = new double[6];

Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

don't be lazy ass;)
• 02-13-2010, 07:26 PM
binarzt
Oh sorry,

But when I need a array, where I don't know beforehand, how many elemnts I need, how can I do that ?

And why don't I need something like this in the main method for this "a" array there ?

Sorry to bother you ^^
• 02-13-2010, 10:00 PM
Fubarable
Quote:

Originally Posted by binarzt
But when I need a array, where I don't know beforehand, how many elemnts I need, how can I do that ?

The method polynom will know at the time it is called the size of the array passed to it (which it can get by calling a.length).

Quote:

And why don't I need something like this in the main method for this "a" array there ?

Quote:

Sorry to bother you ^^
I don't see you bothering anyone here.

Oh, by the way, I hope you don't mind that I added code tags to the code in your first post to make it easier to read. To learn how to do this yourself, please see my signature below. And welcome to the forum.

edit: I see sort of what you're asking. The array "a" in the main method is obtained as the array returned by the polynom method, so you don't need to construct it at all. In other words, there's no need for

Code:

`double[] a = new double[6];`

since you're getting a's object from the polynom method:
Code:

`double[] a = polynom(c);`

Much luck.
• 02-14-2010, 10:01 AM
binarzt
Quote:

Originally Posted by Fubarable
The method polynom will know at the time it is called the size of the array passed to it (which it can get by calling a.length).

Here in this method it was the case, that the needed array was as long as a.length.

But I meant, if you got a method, where you don't know before hand, how many elements it will have at the end, how can I construct that ? Here is my example:

Code:

```  public static int [] a5e (int x,int y,int a)   {     int [] temp;     int zaehler = 0;         for (int i = 0; (i*x) <= a; i++)           {           for (int j = 0; (j*y) <= a; j++)               {                   if ( a == i*x + j*y)                     {                           temp[zaehler]=i;                           zaehler++;                           temp[zaehler]=j;                           zaehler++;                         }                   }           }         return temp;   }```
I don't know exactly, how many elements temp will get ?

I read the Java Tutorials and some other help pages for arrays, but I think, I still don't understand why it sometimes works just fine with arrays, and sometimes don't.

Greetz