Comparing two java arrays???? anyone help?
-
heres the problem... my first array is Tim[] i type in its size(including the second array) and i inputted 4 elements....[1,2,2,4] the second array Moi[] consists of the elements[1,2,3,4] when i compare them regarding if they have equal elements or the same elements on both of the arrays...... when i print out the output in another array Joe[]....the output is this 1 2 2 4 i mean, how come there's two 2's???? is there any way to solve this?? i just want 1,2,4 to be displayed... am i missing something??? please help me out JavaAdicts.... thank you...
-
Answer:
The reason you get two 2's is that Tim contains two 2's. Wrather than using an array to store the common numbers, use a set. Sets (i.e. TreeSet) do not allow duplicates: For example: int[] vals = {1, 2,2,4,5,5,6,6,6,6,6}; TreeSet<Integer> s = new TreeSet<Integer>(): for(int i : vals){ s.add(i); } for(int i : s){ System.out.println(i); } //WIll print out the followin numbers in no particular number: 1, 2, 4, 5, 6. Now, if you need the outputted values to be in sorted order, you can add a list to the mix: int[] vals = {1, 2,2,4,5,5,6,6,6,6,6}; TreeSet<Integer> s = new TreeSet<Integer>(): for(int i : vals){ s.add(i); } LinkedList<Integer> list = new LinkedList<Integer>(); list.addAll(s); Collections.sort(list); for(int i : list){ System.out.println(i); } //WIll print the same numbers as above but you are guaranteed they will be in sorted order.
max g at Yahoo! Answers Visit the source
Other answers
Well, i have got a running example for you. I hope you will find your answer here. http://www.java-examples.com/compare-two-java-int-arrays-example other links for good code java sites, http://www.java2s.com/Tutorial/Java/0040__Data-Type/CompareTwoJavaintArrays.htm
Related Q & A:
- Can anyone help me do a website?Best solution by Yahoo! Answers
- Can anyone help me get directions to Commonwelath park in beaverton oregon?Best solution by Yahoo! Answers
- Can anyone help me fix my MSN Keyboard Trouble?Best solution by Yahoo! Answers
- Could anyone help me out has to do with workers comp. i received a check stating permanent partial disability?Best solution by Yahoo! Answers
- Can anyone help me with Windows Automatic Updates?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.