# make2 challenge

Hi,

I am trying below challenge

make2
http://codingbat.com/prob/p143461

i have not understood the requirement.
Given 2 int arrays, a and b, return a new array length 2 containing, as much as will fit, the elements from a followed by the elements from b. The arrays may be any length, including 0, but there will be 2 or more elements available between the 2 arrays.

make2({4, 5}, {1, 2, 3}) → {4, 5}
make2({4}, {1, 2, 3}) → {4, 1}
make2({}, {1, 2}) → {1, 2}
ozo

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
return new int[]{a.length>0?a[0]:b[0],a.length>1?a[1]:b[1-a.length]};

elements from a followed by the elements from b:
{4, 5}, {1, 2, 3} → {4, 5,1, 2, 3}
{4}, {1, 2, 3})→ {4, 1,  2, 3}
{}, {1, 2} → {1, 2}
as much as will fit in in a new array length 2:
{4, 5,1, 2, 3 } → {4, 5}
{4, 1,  2, 3}→ {4, 1}
{1, 2} → {1, 2}

this is clear

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int[2];
int[] d=new int[2];
d=a+b;
int[] e=new int[2];
e={d[0],d[1]}
return e;

}``````

i tried like this but not working

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int[2];
int[] d=new int[2];
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int[2];
e[0]=list[0];
e[1]=list[1];
return e;

}``````

i tried as above and got below error. Please advise on how to fix it.

What is the best way to add two array so that i can get its first two elements from resultant array?

return new int[]{a.length>0?a[0]:b[0],a.length>1?a[1]:b[1-a.length]};

i di dnot understand above solution. why we are only checking a>0 and a>1 etc not b>0 or b>1 etc.

Since we are guaranteed that "there will be 2 or more elements available between the 2 arrays.",
we can know that negation of the guards a.length>0 and a.length>1 are sufficient to guarantee that b[0] or b[1-a.length] are valid

there will be 2 or more elements available between the 2 arrays

what it mean by above sentence. I am not clear on that. please advise

when i try as below getting below error

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int[2];
int[] d=new int[2];
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int[2];
e[0]=list[0];
e[1]=list[1];
return e;

}``````

Compile problems:

Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined

see Example Code to help with compile problems

Also cannot we add one array with other array simply without converting to List and then add as below?

// d=a+b;//why this is not possible some waht like this to resolve this challenge
List<String> list = new ArrayList<String>(Arrays.asList(a));

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int[2];
int[] d=new int[2];
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int[2];
e[0]=list[0];
e[1]=list[1];
return e;

}``````

i thought this solution should work 100% but i got below error

Compile problems:

Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined

see Example Code to help with compile problems

is it beacuase codingbat site wont support ArrayList?

``b[1-a.length]};``

i am not clear on above. Why are we dependent on a withing b array. please advise

we can know that negation of the guards a.length>0 and a.length>1 are sufficient to guarantee that b[0] or b[1-a.length] are valid

what it means by negation of guards?

``````return new int[]{a.length>0?a[0]:b[0],a.length>1?a[1]:b[1-a.length]};
``````

how above solution is working when a length is 0. please advise
make2({}, {1, 2}) → {1, 2}
when a length is 0
a.length>0 is not true, so a.length>0?a[0]:b[0] is b[0]
a.length>1 is not true, so a.length>1?a[1]:b[1-a.length] is b[1-a.length] == b[1-0] == b[1]
so we return new int[]{b[0],b[1]} as required

b[1-a.length]

i never got this idea. Is there is any alternate, simple approach to resolve this challenge. Please advise

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int[2];
int[] d=new int[2];
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int[2];
e[0]=list[0];
e[1]=list[1];
return e;

}``````
i thought above solution should work 100% but i got below error

Compile problems:

Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined

see Example Code to help with compile problems

is it beacuase codingbat site wont support ArrayList? cannot i use arraylist in that site? please advise
public int[] make2(int[] a, int[] b) {
List<Integer> list=new ArrayList<Integer>(a.length+b.length);
for( int i:a ){
}
for( int i:b ){
}
int[] e=new int[2];
e[0]=list.get(0);
e[1]=list.get(1);
return e;
}

``````  public int[] make2(int[] a, int[] b) {
List<Integer> list=new ArrayList<Integer>(a.length+b.length);
for( int i:a ){
}
for( int i:b ){
}
int[] e=new int[2];
e[0]=list.get(0);
e[1]=list.get(1);
return e;
}``````

i like this solution close to what i am thinking with ArrayList approach. But i still do not understand why i fail with below code

``````public int[] make2(int[] a, int[] b) {
//int[] c=new int[2];
int[] d=new int[2];
// d=a+b;
List<String> list = new ArrayList<String>(Arrays.asList(a));
int[] e=new int[2];
e[0]=list[0];
e[1]=list[1];
return e;

}

``````

i get below error
Compile problems:

Error:      List<String> list = new ArrayList<String>(Arrays.asList(a));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The constructor ArrayList<String>(List<int[]>) is undefined

see Example Code to help with compile problems

i wonder why error messages are not so obviously clear to me but looks some gibberish.
what they meant constroctor not defined?

How to fix my code so that i can make use of Arrays.asList.

``````import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestArrayEx {
public static void main(String[] args) {
int a[] = { 4, 5 };
int b[] = { 1, 2, 3 };

/*
* Integer c[]= toObject(a); Integer d[]=toObject(b);
*/

System.out.println("-->" + make2(a, b)[0] + "-->" + make2(a, b)[1]);

}

public static int[] make2(int[] a, int[] b) {

Integer c[] = toObject(a);
Integer d[] = toObject(b);
List<Integer> list = new ArrayList<Integer>(Arrays.asList(c));

/*
* int[] result = new int[IntegerArray.length]; for (int i = 0; i <
* IntegerArray.length; i++) { result[i] = IntegerArray[i].intValue(); }
* return result;
*/

// Integer list2[] = new Integer[arrlist.size()];
// list2 = arrlist.toArray(list2);

/*
* int[] result = new int[list.length]; for (int i = 0; i <
* IntegerArray.length; i++) { result[i] = IntegerArray[i].intValue(); }
* return result;
*/
Integer list2[] = new Integer[list.size()];
list2 = list.toArray(list2);

int[] e = new int[2];
e[0] = list2[0];
e[1] = list2[1];
return e;

}

public static Integer[] toObject(int[] intArray) {

Integer[] result = new Integer[intArray.length];
for (int i = 0; i < intArray.length; i++) {
result[i] = Integer.valueOf(intArray[i]);
}
return result;
}

}``````

above worked. Not sure right way of doing or not. Please advise.

I still cannot run in codingbat as i have to move toObject method inside though

``````public int[] make2(int[] a, int[] b) {
Integer c[] = toObject(a);
Integer d[] = toObject(b);
List<Integer> list = new ArrayList<Integer>(Arrays.asList(c));
Integer list2[] = new Integer[list.size()];
list2 = list.toArray(list2);

int[] e = new int[2];
e[0] = list2[0];
e[1] = list2[1];
return e;
}

public Integer[] toObject(int[] intArray) {
Integer[] result = new Integer[intArray.length];
for (int i = 0; i < intArray.length; i++) {
result[i] = Integer.valueOf(intArray[i]);
}
return result;
}

``````

i refactored as above and passed all tests. Please advise if above approach is correct?