0

I found this snippet online that made a array_intersect like function in java using integer.

Sample:

        int[] intersect(int[] arr1, int[] arr2) {
            int count = 0;
            for(int a = 0; a < arr1.length; a++) {
                for(int b = 0; b < arr2.length; b++) {
                    if(arr1[a] == arr2[b]) {
                        count++;
                        break;
                    }
                }
            }

            int[] result = new int[count];
            count = 0;
            for(int a = 0; a < arr1.length; a++) {
                for(int b = 0; b < arr2.length; b++) {
                    if(arr1[a] == arr2[b]) {
                        result[count++] = arr1[a];
                        break;
                    }
                }
            }

            return result;
        }

        int[] arr1 = new int[] {10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
         95, 85, 75, 65, 55, 45, 35, 25, 15, 05,
         10, 15, 20, 25, 30, 35, 40, 45, 50, 55};

        int[] arr2 = new int[] {15, 25, 35, 45, 55,
         12, 22, 32, 43, 52,
         15, 25, 35, 45, 55};

        int[] p1 = this.unique(arr1);
        int[] p2 = this.unique(arr2);
        int[] intersectResults = this.intersect(arr1, arr2);

            for(int a = 0; a < intersectResults.length; a++) {
                System.out.print(intersectResults[a] + "    ");
            }

But when i changed it to:

        String[] intersect(String[] a_yourname, String[] a_crushname) {
            int count = 0;
            for(int a = 0; a < a_yourname.length; a++) {
                for(int b = 0; b < a_crushname.length; b++) {
                    if(a_yourname[a] == a_crushname[b]) {
                        count++;
                        break;
                    }
                }
            }

            String[] result = new String[count];
            count = 0;
            for(int a = 0; a < a_yourname.length; a++) {
                for(int b = 0; b < a_crushname.length; b++) {
                    if(a_yourname[a] == a_crushname[b]) {
                        result[count++] = a_yourname[a];
                        break;
                    }
                }
            }

            return result;
        }

     String[] flames = this.intersect(a_yourname, a_crushname);
            //String[] p23 = this.unique(arr2);
             System.out.println("heheh" +flames.length);
             for(int a = 0; a < flames.length; a++) {
                System.out.print(flames[a] + "    ");
            }

Can someone explain to me what I did wrong here? I'm really not familiar with Java.

a_yourname and a_crushname are both string arrays.

3
  • 2
    Are you using == to compare two strings? The cleaner and potentially faster method is to put one array into set and another into some collection (e.g. Arrays.asList()) and call retainAll() on the set. Commented Jan 26, 2013 at 10:59
  • @billc.cn can you give an example of that? i just got that code form the net not sure how that works :) Commented Jan 26, 2013 at 11:27
  • Putting Java code incorrectly in a JSP file instead of a Java class doesn't make it a JSP problem. You'd have had exactly the same problem when putting that Java code in a normal Java class with a main() method. I removed the non-related JavaEE/JSP noise from the question. Commented Jan 26, 2013 at 12:40

1 Answer 1

1

Code:

    String[] a = new String[] { "aa", "bb", "cc" };
    String[] b = new String[] { "bb", "cc", "dd" };

    Set<String> setA = new HashSet<String>(Arrays.asList(a));
    List<String> listB = Arrays.asList(b);
    setA.retainAll(listB);
    System.out.println(setA);

You can see the results here: http://ideone.com/tKg5xa

If you really want the result as an array:

String[] out = setA.toArray(new String[setA.size()]);

Again there's no need to use an array if all you want is to iterate through the result. Google "Java for each" or pick up a Java book.

Sign up to request clarification or add additional context in comments.

8 Comments

Why is there a new String[]? Is it necessary or is just a choice of declaration of array?
No, it's not strictly necessary in this case. It's just an old habit.
is setA returned as an Array?
No, it's a Set. Use Set.toArray() to get an array, but there's really no need to do that. Most cases Collections works better than Arrays in modern Java.
thank you for this it really works great, but regarding the code above do you understand most of it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.