0
    arr.add(mas[0]);
    for(int i=1;i<mas.length;i++) {
        for(int j=0;j<arr.size();j++) {
            if(mas[i].equals(arr.get(j))||mas[i].equals("")){
                break;
            }
            arr.add(mas[i]);
        }
    }
    for(int j=0;j<arr.size();j++) { 
        System.out.print(arr.get(j)+";");
    }

enter image description here

i dont understand what is wrong

0

3 Answers 3

1

Add the value from mas, only if the inner-loop does not find a match.

boolean b;
arr.add(mas[0]);
for(int i=1;i<mas.length;i++) {
    b = false;
    for(int j=0;j<arr.size();j++)
        if(mas[i].equals(arr.get(j))||mas[i].equals("")){
            b = true;
            break;
        }
    if (!b) arr.add(mas[i]);
}
for(int j=0;j<arr.size();j++) {
    System.out.print(arr.get(j)+";");
}

Output

car;cycle;bomj;

Ideally, just use the List#contains method.
And, utilize the String#join method to print the concatenated values.

String s;
arr.add(mas[0]);
for(int i=1;i<mas.length;i++)
    if (!arr.contains(s = mas[i])) arr.add(s);
System.out.println(String.join(";", arr));

Output

car;cycle;bomj
Sign up to request clarification or add additional context in comments.

Comments

0

If I have interpreted good your problem, you should add the item in the list only after the second loop.

arr.add(mas[0]);
outer: for(int i=1;i<mas.length;i++) {
    if ("".equals(mas[i])) {
        continue;
    }
    for(int j=0;j<arr.size();j++) {
        if(mas[i].equals(arr.get(j))){
                break outer;
            }
        }
        arr.add(mas[i]);
    }
    for(int j=0;j<arr.size();j++) { 
        System.out.print(arr.get(j)+";");
    }

I have edited the answer adding a label on the outer loop named "outer" and made some minor changes to make it more readable.

2 Comments

i think my code is wrong, i want to make (.contains) but without (.contains)
and every new word in "String" will be (.add) once, if array see word again dont (.add)
-1
    arr.add(mas[0]);    
    int raz=0;
    for(String word : mas ) {
        raz=0;
        for(int j=0;j<arr.size();j++) {
            if(word.equals(arr.get(j))||word.equals("")){
                raz--;
                break;
            }
            raz++;
        }
        if(raz==arr.size()){
            arr.add(word);
        }
    }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.