0

Here is how my constructor has been defined

    public One (int [] a, int [] b)
{
    A = a;
    B = b;
    C = new int [a.length + b.length];
}

This is how I am creating a object for this:

    One A = new One ([1,3,5], [1,5,3]);

I am getting multiple errors for this , like the constructor (int,int,int,int,int,int) is not defined and syntax error on tokens,delete these tokens.

Can someone please tell me where am i going wrong ? Thanks

2
  • sorry guys having some issues how to insert code here, i am editing it to proper format, thanks Commented Nov 27, 2012 at 21:24
  • You have you specify the type when creating an array (and create an instance); new int[] {1, 3, 5} Commented Nov 27, 2012 at 21:25

2 Answers 2

5

This is not how you create an array in java. You need to use it like this: -

One A = new One (new int[] {1,3,5}, new int[] {1,5,3});

new int[] creates an integer array object.

{1, 3, 5} initializes the array inline.


As a side note, you should declare your variable starting with lowercase letter. In your code, your instance array reference should be a instead of A. And use this.a to access it to avoid name conflict between local and instance variable.

Sign up to request clarification or add additional context in comments.

5 Comments

Gotta get just a little bit quicker... :)
@ppeterka.. haha :) Actually it took 1 and a half minutes to answer. ;)
I bet it took <=30 seconds to answer, and the other 1 minute polishing the answer to be real good. (I even needed to polish this comment a couple if times...)
@ppeterka. lol ;) Yeah of course. Polishing is more than required.
@GanGnaMStYleOverFlowErroR.. That you guessed right. ;) Actually I'm allowed to type only 1 comment per 15 seconds, so I'm a bit late on this. lol ;)
3

your syntax for creating anyonomous array is wrong. you create an anyonomous array like this.

One A = new One (new int []{1,3,5}, new int[]{1,5,3});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.