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
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
return new int[]{a[0],a[1],b[0],b[1]};
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
@gudii9 : how would you rate your rate or progress on these challenges to date? Do you think you are making much progress?
0
gurpsbassiCommented:
Classic array copy solution:
Might be overkill if you're arrays are only of size 2 but its generic.

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

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

CEHJCommented:
Except stupidly, codingbat won't allow arraycopy
0
krakatoaCommented:
But it does allow Arrays.copyOfRange().
0
gudii9Author Commented:
@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
0
gudii9Author Commented:
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
0
krakatoaCommented:
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.
0
gurpsbassiCommented:
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.
0
gudii9Author Commented:
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
0
gurpsbassiCommented:
mutate meaning it changes the contents of the original array you pass in.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.