2

It's my first question here and it's about Java. I would like to implement the following logic:

I have got two string Arrays(or string Lists of string). There is an array of strings (asu) - M1, M2, M3 ... As well as an array of string (rzs) - M1, M2, M3 and all possible combinations thereof. The need for each element (asu) (for example M1) to find an element in (rzs) (M1, M1M2, ..), which contains (e.g. M1). Example: took M1 from (asu) and will start search for duplicate(contain) in (rzs). We found M1M2 in (rzs), it contains M1. After that we should delete both elements from arrays(lists). And I'm sorry for my English skills^^

String[] asu = { "M1", "M1", "M1", "M3", "M4", "M5", "M1", "M1", "M1", "M4", "M5", "M5" };
String[] rzs = { "M1", "M2", "M3", "M4", "M5", "M1M2", "M1M3", "M1M4", "M1M5", "M2M3", "M2M4", "M2M5", "M3M4", "M3M5", "M4M5", "M1M2M3", "M1M2M4",
        "M1M2M5", "M1M3M4", "M1M3M4", "M1M4M5", "M2M4", "M2M5" };

public static void main(final String[] args) {
    work bebebe = new work();
    bebebe.mywork();
}

public void mywork() {

    System.out.println(Arrays.deepToString(rzs));
    System.out.println(Arrays.deepToString(asu));
    for (int i = 0; i < asu.length; i++) {
        System.out.println("Итерация: " + i);
        for (int j = 0; j < rzs.length; j++) {
            if (asu[i].matches(rzs[j].toString())) {
                System.out.println(i + " элемент (" + asu[i] + ") в ASU равен " + j + " элементу (" + rzs[j] + ") в RZS");
                asu[i] = "";
                rzs[j] = "";
            }
        }
    }
}

The result does not delete items that are substring. Does not satisfy logic. I will appreciate your advice.

1 Answer 1

2

if you'll use lists you'll have a lot better: the delete will not ask you to take the rest of the array back, and the use of lists means less logic on your code

List<string> asu = Arrays.asList("M1","M1","M1","M3","M4","M5","M1","M1","M1","M4","M5","M5");
List<string> rzs = Arrays.asList("M1","M2","M3","M4","M5",
"M1M2","M1M3","M1M4","M1M5","M2M3","M2M4","M2M5","M3M4","M3M5","M4M5"
,"M1M2M3","M1M2M4","M1M2M5","M1M3M4","M1M3M4","M1M4M5","M2M4","M2M5");
public static void main(String[] args) {
work bebebe = new work();
bebebe.mywork();
}

public static void mywork() {
   ArrayList<String> tmp1 = new ArrayList<String>();  
   ArrayList<String> tmp2 = new ArrayList<String>();  
   System.out.println((rzs));
   System.out.println((asu));
   for (String curr : asu){
     for (String currRzs : rzs){
       if (currRzs.contains(curr)) {
          System.out.println(" item("+curr+") in ASU found contained in ("+currRzs+") in RZS");

      if(tmp1.contains(curr) == false)
         tmp1.add(curr);

      if(tmp2.contains(currRzs) == false)
         tmp2.add(currRzs);
       }
      } 
    }

   for (String curr : tmp1){
      asu.remove(curr);
   }

   for (String curr : tmp2){
      rzs.remove(curr);
   }
}
Sign up to request clarification or add additional context in comments.

13 Comments

Your code is beatiful, but your solution also don't satisfy logic. As i said before, if we found M1M2 in (rzs) we should compare him with M1. And if M1 is substring of M1M2 we should delete both. Your code doesn't do it. "Contain" statement only comparing object like M1=M1, but not strings of objects;
@EldarNezametdinov contains does equals. you can box string to your own class and override equals to do indexOf. do you want an example of it?
if it not difficult for you, because i have started learn Java only few days ago... i don't know how do it right for this situation.
program always crushed by java.util.AbstractList,it should be because we update List + curr is already defined, i changed it to -> curr2 and rzs.remove(curr) is ok? I can't pass a parameter currRzs
@EldarNezametdinov i forgot to close the "for" loop, i've edit it and now it shouldn't crash
|

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.