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}
please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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:
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}

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
ozoCommented:
return new int[]{a.length>0?a[0]:b[0],a.length>1?a[1]:b[1-a.length]};
gudii9Author Commented:
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

gudii9Author Commented:
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;
  
  }

Open in new window


i tried like this but not working
gudii9Author Commented:
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;
  
  
  
  
  }

Open in new window


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?
gudii9Author Commented:
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.

Please advise
ozoCommented:
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
gudii9Author Commented:
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));
    list.addAll(Arrays.asList(b));
  int[] e=new int[2];
  e[0]=list[0];
  e[1]=list[1];
  return e;
  
  
  
  
  }

Open in new window


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));
    list.addAll(Arrays.asList(b));


please advise
gudii9Author Commented:
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;
  
  
  
  
  }

Open in new window


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?

Please advise
gudii9Author Commented:
b[1-a.length]};

Open in new window


i am not clear on above. Why are we dependent on a withing b array. please advise
gudii9Author Commented:
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?

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

Open in new window


how above solution is working when a length is 0. please advise
ozoCommented:
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
gudii9Author Commented:
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));
    list.addAll(Arrays.asList(b));
  int[] e=new int[2];
  e[0]=list[0];
  e[1]=list[1];
  return e;
  
  
  
  
  }

Open in new window

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
ozoCommented:
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;
}
gudii9Author Commented:
  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;
}

Open in new window


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));
    list.addAll(Arrays.asList(b));
  int[] e=new int[2];
  e[0]=list[0];
  e[1]=list[1];
  return e;
  
  
  
  
  }
  
  
  

Open in new window


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.

please advise
gudii9Author Commented:
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;
	}

}

Open in new window


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
gudii9Author Commented:
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;
	}

  
  
  
  
  
  
  

Open in new window


i refactored as above and passed all tests. Please advise if above approach is correct?
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
Programming

From novice to tech pro — start learning today.