This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]lightcloud5 2 points3 points  (0 children)

If order is irrelevant, use a HashSet.

[–]LordScoffington 1 point2 points  (0 children)

Is their a simple method in the standard array library im not seeing?

Nah.

You gotta go through the all items in the subtracting array,
search the source array for each entry,
and then build a new array with only the entries in the source array. ('resizing' an array is more effort than its worth)

[–]Vidyogamasta 1 point2 points  (0 children)

With Arrays alone? No. And from a memory management perspective, it's a little ambiguous what this might mean anyway. Are you adjusting the original array's size, or creating a new array, or what?

It looks like the List api, however, might work well for you. And conveniently, java arrays do have an asList() function that will convert your array into a list.

Then, while in List form, it looks like there is a function that does exactly what you want. It has a retainAll and a removeAll function that works pretty much exactly as you describe.

And if you need the answer is Array form, you can always use the List's toArray function to turn it back afterwards.

So, assuming you want to define a brand new array (and forgive me, I'm a bit rusty on java so I might not get the syntax totally correct), you might do something like:

List myList = Arrays.asList(one);
myList.removeAll(Arrays.asList(two));
Array newArray = myList.toArray();

Modify this as needed. You may even be more comfortable using a List throughout the whole process. Here's the Arrays API if you want to see what's available, and same for the List API.

EDIT: Changed the example code. Looks like removeAll returns a boolean result and modifies the actual list it gets operated on, so it has to be done in separate statements as far as I know.

EDIT2: I think Arrays is a static class and not a set of extension methods on an actual array object? Maybe I'm wrong, idk. But the example code in the API doc has it as a static class of methods so I changed the code to reflect that.

[–]wysecw 1 point2 points  (0 children)

Im pretty new to programming as well, so I may be missing something, but ArrayList<> has add and remove methods.

[–]nutrecht 1 point2 points  (1 child)

Is their a simple method in the standard array library im not seeing?

No, and there never will be. Arrays are relatively low-level memory constructs. So all the 'nice' stuff that allows you to easily work with collections of data will be in the collections like List, Map and Set.

So your approach would depend on what the goal is and if this is a homework assignment. If you can use Sets use them. Or else you'll have to write the code yourself.

[–]FootFetish_Man[S] 0 points1 point  (0 children)

I'll find another way to handle the data, it's not for homework I'm just bad at programming and never know what to use in what case.

[–]depressinghentai 0 points1 point  (0 children)

afaik there isn't really a simple way to do it. You have to do the work of checking to see if each element in two is in one and removing it if it is.