Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

plusTwo challenge

Hi,

I am working on below challenge
http://codingbat.com/prob/p180840

i wrote as below

public int[] plusTwo(int[] a, int[] b) {
int len1=a.length;
int len2=b.length;
int len=len1+len2;

int[] c=new int[len];

c[0]=a[0];
c[1]=a[1];
c[2]=b[0];
c[3]=b[1];
return c;
  
}

Open in new window


i passed all tests. How to improve my code and alternate ways of doing it. please advise
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@gudii9 : how would you rate your rate or progress on these challenges to date? Do you think you are making much progress?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Except stupidly, codingbat won't allow arraycopy
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

@gudii9 : how would you rate your rate or progress on these challenges to date? Do you think you are making much progress?
i think so. I am passing about 70% of challenges on my own. But i still has lot of room to improve in writing simple yet power code like you guys all writing
Avatar of gudii9

ASKER

public int[] plusTwo(int[] a, int[] b) {
final int[] combo = new int[a.length + b.length];
System.arraycopy(a, 0, combo, 0, a.length);
System.arraycopy(b, 0, combo, a.length, b.length);

}

above gave below error
Error:Bad code
Common problems: code should not use println or class or static or exceptions

what is combo in above method.
when to use Arrays.copyOfRange(). and System.arraycopy.

Please advise
when to use Arrays.copyOfRange(). and System.arraycopy.

this issue and the others you mention, are shortcomings on Codingbat site - it cannot cope with those issues like System calls. In a proper dev environment, System.arrayCopy and Arrays.copyOfRange will work fine.


BTW - CEHJ already answered this point once.
above gave below error
Error:Bad code
Common problems: code should not use println or class or static or exceptions

@CEHJ has already explained that the codingbat website doesn't allow the code.

what is combo in above method.
errr umm,  its a name of a variable. call it what you want.

when to use Arrays.copyOfRange(). and System.arraycopy
One returns an array whilst the other mutates an array you give it.

I haven't bench marked the performance so can't say which is faster. I would imagine that Arrays.copyOfRange perhaps uses System.arrayCopy under the covers but you would need to check that.
Avatar of gudii9

ASKER

One returns an array whilst the other mutates an array you give it.

what it mean by mutates any simple example explaining these two concepts?

please advise
mutate meaning it changes the contents of the original array you pass in.