Results 1 to 6 of 6

Thread: Array problem

  1. #1
    binarzt is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default 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 added
    Last edited by Fubarable; 02-13-2010 at 08:57 PM. Reason: code tags added

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
        public static double[] polynom(double[] a)
        {
            double[] b = //{0};
                         new double[a.length];

  3. #3
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    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;)

  4. #4
    binarzt is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by binarzt View Post
    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).

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

    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

    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.

  6. #6
    binarzt is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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:

    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 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

Similar Threads

  1. Problem with using an array in a constructor
    By planesinspace in forum New To Java
    Replies: 3
    Last Post: 08-28-2009, 09:17 AM
  2. A problem in practicing the array
    By dl21 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 11:32 PM
  3. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 02:25 AM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •