Results 1 to 6 of 6
Thread: Array problem
- 02-13-2010, 06:07 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
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:
Java 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.
Thanks in advance.
Greetz
Moderator edit: code tags addedLast edited by Fubarable; 02-13-2010 at 08:57 PM. Reason: code tags added
- 02-13-2010, 06:15 PM #2
Java Code:public static double[] polynom(double[] a) { double[] b = //{0}; new double[a.length];
- 02-13-2010, 06:24 PM #3
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
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.
Maybe you need this instead:
double [] b = new double[6];
Read about array initialization in java:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
don't be lazy ass;)
- 02-13-2010, 06:26 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
Oh sorry,
I just read your comment now, thanks.
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 ^^Last edited by binarzt; 02-13-2010 at 06:28 PM.
-
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).
Huh? Please elaborate.And why don't I need something like this in the main method for this "a" array there ?
I don't see you bothering anyone here.Sorry to bother you ^^
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
Java Code:double[] a = new double[6];
since you're getting a's object from the polynom method:
Java Code:double[] a = polynom(c);
Much luck.Last edited by Fubarable; 02-13-2010 at 09:03 PM.
- 02-14-2010, 09:01 AM #6
Member
- Join Date
- Feb 2010
- Posts
- 3
- Rep Power
- 0
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:
I don't know exactly, how many elements temp will get ?Java 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 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
Similar Threads
-
Problem with using an array in a constructor
By planesinspace in forum New To JavaReplies: 3Last Post: 08-28-2009, 09:17 AM -
A problem in practicing the array
By dl21 in forum New To JavaReplies: 2Last Post: 04-24-2008, 11:32 PM -
array problem
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-05-2008, 02:25 AM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks