English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

to return a new array that contains all the elements of the array a not in b.

Public static int [] remove (int[] a, int[] b)

For example, if the array a is {5,3,2,7,1} and the array b is {8,2,5} the remove method should return the array {3,7,1}.

The method should function properly for arrays of any lengths.

2007-11-26 06:54:20 · 3 answers · asked by programhelp 2 in Computers & Internet Programming & Design

3 answers

You didn't really phrase this as question, but I am assuming that you want someone to write the method for you? I won't write it for you, but I can give you some pointers. If I was doing it, I would pass the arrays to a method (as you have), then in that method, I would declare an array list to hold the elements not present in both arrays. The array list is used because it is dynamic, and not fixed in size as a normal array is. Then, I would create a loop within a loop, the outside loop iterating for the number of members in the first array, and the inside loop iterating for the number of items in the second array. For each pass of the loop, I would look for a match of the element in array1 to an element in array2. I would have a flag, and if there was not match, I would set the flag to "true", then check it after exiting the insode loop, and add the item to the array list if the flag is true. That's what I would do - I'm sure there are other ways, but this would work. Good luck : )

2007-11-26 07:11:17 · answer #1 · answered by Anonymous · 0 0

As you loop through each array comparing to the other, why not use the ArrayList.contains() method? Put it in a conditional check and build your new int array with the results as needed.

2007-11-26 07:10:35 · answer #2 · answered by Jim Maryland 7 · 0 0

Hi, here is what i come up with, please ask anything else if you need more explanation, or rate my answer if it helped. 1 - Class A with no changes: public class A { ArrayList c; public A() { c = new ArrayList(); } public void add(C neww) { c.add(neww); } public C get(int i) { C get = c.get(i); return get; } } 2 - Class B - two things changed: 1 - In the constructor you need to initialize (private A myC) its not initialized, and its always null. 2 - In the method (addToArrayInA) you are calling (A.add(newC);) where A is the class name, and must be the instance of this class. so here is the class: public class B { private A myC; public B() { myC = new A(); } public void addToArrayInA(int one, String two) { C newC = new C(one, two); //A.add(newC); myC.add(newC); } public C getC() { int i = 0; C cc = myC.get(i); return cc; } } 3 - Class C: a toString() method is added to this class, just to make sure that we are doing the right thing: public class C { private int o; private String t; public C(int one, String two) { o = one; t = two; } @Override public String toString() { return "C [o=" + o + ", t=" + t + "]"; } } 4 - A testing class which may help you: public class Main { public static void main(String[] args) { B b = new B(); b.addToArrayInA(1, "String"); C c = b.getC(); System.out.println(c.toString()); } } In this class: 1 - you create an instance of class b so you can call the method (addToArrayInA) which adds a new c to the list in A. 2 - Create a new instance of C, which holds the value of the C object in the list in A (little confusing but this is it). 3 - Print the value of C, using the earlier overridden toString() method. Hope this helps.

2016-05-26 00:43:23 · answer #3 · answered by nakita 3 · 0 0

fedest.com, questions and answers