Avatar of gudii9
gudii9
Flag for United States of America asked on

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
ProgrammingJavaProgramming Languages-Other

Avatar of undefined
Last Comment
gudii9

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ozo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ozo

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

ASKER
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
gudii9

ASKER
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
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
gudii9

ASKER
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?
gudii9

ASKER
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
ozo

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
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
gudii9

ASKER
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
gudii9

ASKER
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
Your help has saved me hundreds of hours of internet surfing.
fblack61
gudii9

ASKER
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
gudii9

ASKER
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
ozo

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
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
ozo

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;
}
gudii9

ASKER
  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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
gudii9

ASKER
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
gudii9

ASKER
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?