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.please advise
make2({4, 5}, {1, 2, 3}) → {4, 5}
make2({4}, {1, 2, 3}) → {4, 1}
make2({}, {1, 2}) → {1, 2}
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}
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;
}
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));
list.addAll(Arrays.asList(b));
int[] e=new int[2];
e[0]=list[0];
e[1]=list[1];
return e;
}
return new int[]{a.length>0?a[0]:b[0],a.length> 1?a[1]:b[1 -a.length] };
there will be 2 or more elements available between the 2 arrays
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));
list.addAll(Arrays.asList(b));
int[] e=new int[2];
e[0]=list[0];
e[1]=list[1];
return e;
}
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));
list.addAll(Arrays.asList(b));
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
b[1-a.length]};
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
return new int[]{a.length>0?a[0]:b[0],a.length>1?a[1]:b[1-a.length]};
make2({}, {1, 2}) → {1, 2}when a length is 0
b[1-a.length]
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));
list.addAll(Arrays.asList(b));
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 public int[] make2(int[] a, int[] b) {
List<Integer> list=new ArrayList<Integer>(a.length+b.length);
for( int i:a ){
list.add(i);
}
for( int i:b ){
list.add(i);
}
int[] e=new int[2];
e[0]=list.get(0);
e[1]=list.get(1);
return e;
}
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));
list.addAll(Arrays.asList(b));
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
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));
list.addAll(Arrays.asList(d));
/*
* 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;
}
}
public int[] make2(int[] a, int[] b) {
Integer c[] = toObject(a);
Integer d[] = toObject(b);
List<Integer> list = new ArrayList<Integer>(Arrays.asList(c));
list.addAll(Arrays.asList(d));
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;
}