Results 1 to 15 of 15
  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Help with ArrrayLists, String Arrays, and similar methods!

    Hello again - I was playing around with code today. I have a problem that I'm trying to solve.

    Long story short, I have a set of code coming up with solutions - the solutions are 4 sets of co-ordinates.

    The issue, is that the code will produce duplicates - the same 4 sets, just in a different order. I need to get rid of duplicates.

    AKA, it will produce 0,0 0,1 1,0 1,1 as a solution, and then 0,0 0,1 1,1 1,0, which is the same thing, but in a different order.

    I need to discard those answers. I have all of those results in stored in a string array. The problem is that I need to check them and delete repeats. How am I supposed to check them, if tokenize works only with string arrays (aka I have to use an array, which is immutable).

    FOR LAZY READERS: How do I do this so it works:
    Java Code:
    for(int i = 0; i < arraylist.size(); i++) {
      String[] tokens = arraylist.get(i).split("[ ]+");
      for(int z = 0; z < arraylist.size(); z++) {
        String[] checktokens = arraylist.get(z).split("[ ]+");
        //compare checktokens[1 && 2] to tokens [1 && 2], and so on, comparing all sets of tokens - I know how to do this part
        if(token pairs are found the same) {
          arraylist.remove(z);
        }
      }
    }
    As far as I understand, that won't work because String[] tokens will try to re declare and initialize itself in every iteration.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    Quote Originally Posted by AlexGraal View Post
    Hello again - I was playing around with code today. I have a problem that I'm trying to solve.

    Long story short, I have a set of code coming up with solutions - the solutions are 4 sets of co-ordinates.

    The issue, is that the code will produce duplicates - the same 4 sets, just in a different order. I need to get rid of duplicates.

    AKA, it will produce 0,0 0,1 1,0 1,1 as a solution, and then 0,0 0,1 1,1 1,0, which is the same thing, but in a different order.

    I need to discard those answers. I have all of those results in stored in a string array. The problem is that I need to check them and delete repeats. How am I supposed to check them, if tokenize works only with string arrays (aka I have to use an array, which is immutable).

    FOR LAZY READERS: How do I do this so it works:
    Java Code:
    for(int i = 0; i < arraylist.size(); i++) {
      String[] tokens = arraylist.get(i).split("[ ]+");
      for(int z = 0; z < arraylist.size(); z++) {
        String[] checktokens = arraylist.get(z).split("[ ]+");
        //compare checktokens[1 && 2] to tokens [1 && 2], and so on, comparing all sets of tokens - I know how to do this part
        if(token pairs are found the same) {
          arraylist.remove(z);
        }
      }
    }
    As far as I understand, that won't work because String[] tokens will try to re declare and initialize itself in every iteration.
    You can use a Set instead of an ArrayList, as far as I know, Sets do not allow duplicate entries.

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    So how would I actually implement them?

  4. #4
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    The problem is that it isn't technically a duplicate - you can think of it as a shape. If the four points are (0,0) (0,1) (1,0) (1,1), this is technically different than (0,0) (1,0) (0,1) (1,1), since different points are in different spots - but they draw the same shape, which is the issue.

  5. #5
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    The way you would implement a Set is identical to ArrayList. And here is a simple example of one.

    Java Code:
    Set<String> set = new HashSet<>();
            set.add("Hello");
            set.add("Hello");
            set.add("Hello");
            set.add("Hello");
            
            System.out.println(set.size());
    You might think this would output 4, the number of times I added to the Set, but it will output 1, due to duplicate removal.

    In addition to AlexGraal, I think a simple iteration would work here, allow me to demonstrate.

    Java Code:
    public boolean isEqual(String[] s1, String[] s2) {
            if (s1.length != s2.length) { //Checks lengths, if not equal, automatically return false, not equal.
                return false;
            }
    
            for (int i = 0; i < s1.length; i++) { //iterates over the length of s1, since s1, and s2 have equal lengths
                if (!s1[i].equals(s2[i])) { // if the loop hits a point where a string in s1 is not equal to the string in the same index of 
                                                                     //s2, the boolean returns false, not equal.
                    return false;
                }
            }
    
            return true; //if the string arrays pass both tests, they are equal.
        }
    Last edited by zFollette; 01-08-2014 at 03:46 AM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    If you had just a set of Points, it would check for duplicates because the Point class overrides hashcode and equals. But what you want is to check for a set of some class of points. Two classes of points of A and B are considered equal if they have the same size and elements regardless of order. This last definition relates to your return value. So you need to have a HashSet of some class which itself constitutes a set points. Then you need to write your own equals and hashcode methods for determining equality of any instance of that class with another. Then the HashSet can properly eliminate duplicates.

    The only difficulty in this is writing the equals method of your set.

    Edit: If your class uses a List implementation it becomes relatively trivial to verify equality based on stated requirements.

    Regards,
    Jim
    Last edited by jim829; 01-08-2014 at 04:56 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    Quote Originally Posted by zFollette View Post
    Java Code:
            for (int i = 0; i < s1.length; i++) { //iterates over the length of s1, since s1, and s2 have equal lengths
                if (!s1[i].equals(s2[i])) { // if the loop hits a point where a string in s1 is not equal to the string in the same index of 
                                                                     //s2, the boolean returns false, not equal.
                    return false;
                }
            }
    
            return true; //if the string arrays pass both tests, they are equal.
        }
    I don't believe that will work because in the OP's message, the following are all considered equal.

    "hello"
    "oellh"
    "oelhl"
    etc.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    Quote Originally Posted by jim829 View Post
    I don't believe that will work because in the OP's message, the following are all considered equal.

    "hello"
    "oellh"
    "oelhl"
    etc.

    Regards,
    Jim
    Ah, I misread

  9. #9
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    Sorry for the late reply but I just though of something, compare the length of two arrays, and if they are equal, check the sum of the arrays, if they are equal, then you're good. This will only work if you are comparing a string array of numerical data, like in your original post.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    Quote Originally Posted by zFollette View Post
    check the sum of the arrays, if they are equal, then you're good
    I don't think so. Array a contains the points (0,0} and {1,1} Array b contains the points {0,1} and {1,0}. The OP needs to look at the List interface and its implementations. The containsAll method, if use properly can help solve the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    Came up with a reasonable solution :)

    Java Code:
      public static boolean check(int x11, int y11, int x22, int y22) {
        if(x11 > x22) {
          return true;
        } else if(y11 > y22) {
          return x11 == x22;
        }
        return false;
      }
    Java Code:
     if(check(x1,y1,x2,y2) && check(x2,y2,x3,y3) && check(x3,y3,x4,y4)) { counter++; }

    Why it works - it only takes the points that are ordered correctly. No need to store all points that work - only the ones ordered correctly!
    Last edited by AlexGraal; 01-08-2014 at 10:29 PM.

  12. #12
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    Quote Originally Posted by jim829 View Post
    I don't think so. Array a contains the points (0,0} and {1,1} Array b contains the points {0,1} and {1,0}. The OP needs to look at the List interface and its implementations. The containsAll method, if use properly can help solve the problem.

    Regards,
    Jim
    '

    Ahh, you got me again.

  13. #13
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    All right. If someone is willing to really think about this, I'd appreciate it. NEW ISSUE

    I just realized that the different shapes can be drawn with the same points - for example:

    (2,0) (1,2) (1,1) (0,0)

    Those points can be connected in two different ways to create different shapes. So what can I do about that? I've been thinking about plotting all possible point combinations - I didn't realize that there are also different ways to connect them.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,930
    Rep Power
    5

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    This can't be resolved until you explain what it is you wish to accomplish with your application. We can always provide an answer A for some subproblem B. But if you tell us later you want to use B to accomplish C, then A may not have been the best answer.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default Re: Help with ArrrayLists, String Arrays, and similar methods!

    All right, fair enough.

    I'm trying to calculate how many possible simple quadrilaterals can be formed inside of a grid of size (x,y).
    I know that in a 2 by 2 grid, 94 quadrilaterals. Unfortunately, my program is returning an answer that is too small, because I completely forgot to take into account the fact that certain combinations of points can create more than one shape, based on how they are connected.

    Currently, my program is producing all possible combinations of points, then taking only the ones that are in a certain order (therefore eliminating the issue of the same points in a different order being seen as a new set of points) and checking that they satisfy the requirements of a simple quadrilaterals:
    1. No three points in a row (aka, 3 points may not be on the same line)
    2. Points must all be individual - no two points on top of each other

Similar Threads

  1. Help with arrays and methods and constructors
    By mjfitches in forum New To Java
    Replies: 1
    Last Post: 02-27-2013, 04:40 PM
  2. Arrays and Methods
    By jmscarlet9 in forum New To Java
    Replies: 3
    Last Post: 03-27-2012, 03:29 AM
  3. Arrays and methods
    By namie in forum New To Java
    Replies: 3
    Last Post: 10-05-2009, 10:43 AM
  4. methods, classes, arrays.. oh my!
    By katalyst in forum New To Java
    Replies: 30
    Last Post: 03-30-2009, 01:57 AM
  5. Arrays & Methods
    By TheRocket in forum New To Java
    Replies: 1
    Last Post: 12-10-2008, 08:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •