Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2008
    Location
    kolkata
    Posts
    21
    Rep Power
    0

    Question initialising Array of object??

    I've a class T which have two integer variables.
    In another class I want to make an array of Term object.

    class Term{
    public int exp=0, coef=0;
    }

    class Polynomial{
    Term array[];
    int i,l;
    Polynomial(int i){
    this.i=i;
    array = new Term[i];
    }
    void SetTerm(int e, int c){
    for(l=0; l<i;l++){
    array[l].exp=e;
    array[l].coef=c;
    }
    }
    /*...
    ...
    */
    }

    now from the main method when I'm calling the
    SetTerm after creating object of Polynomial class...in the output
    a exception is risen: ArrayIndexOutOfBoundsException
    at Polynomial.SetTerm

    So how can i fix the problem of initialize that array??
    please help.........
    Somitesh

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

    Default

    the variable i is not what you expect it to be. For one thing, the "i" you use to initialize the Term array is only visible within the Polynomial constructor. For a safer way to use threads, do this:

    Java Code:
    for (int l = 0; l < array.length; l++) // *** note difference?
    {
      array[l].exp=e;
      array[l].coef=c;
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Can you tell me what are you going to do here.

    Java Code:
    array[l].exp=e;

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    array is a poor choice for a variable name. You should use something that says what it contains.
    For example: Term[] polyTerms = new Term[x];

    Then polyTerms[i].exp = ... would be more readable

  5. #5
    Join Date
    Aug 2008
    Location
    kolkata
    Posts
    21
    Rep Power
    0

    Default

    thanx a lot.......
    Actually I'm a newbie as I told you before.....I'm trying learn this language by heart.......from now I'll give the appropriate name of those variable (Actual name are very big; tough to right again & again but I'll!!).

Similar Threads

  1. initialising a webcam?
    By jonsamwell in forum New To Java
    Replies: 1
    Last Post: 08-25-2008, 05:17 PM
  2. Array of Object
    By haiforhussain in forum New To Java
    Replies: 1
    Last Post: 06-25-2008, 11:24 AM
  3. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:14 AM
  4. Object array query
    By bugger in forum New To Java
    Replies: 5
    Last Post: 01-07-2008, 05:58 PM
  5. how to remove an object from the array list
    By cecily in forum New To Java
    Replies: 3
    Last Post: 08-02-2007, 02:26 PM

Posting Permissions

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