Answer the question
In order to leave comments, you need to log in
How to find the fragment of an array element in Java?
I have an ArrayList, I add rows to it in a loop, all the rows are different, but in some the first and last names are the same. i need to not write the same ones, i use array.contains(string) but it doesn't work, it just skips all the records, what am i doing wrong?
This is what the sheet looks like:
array [0] = 1|Иванов|Иван|sdhfj|fjdkfhjdf|sdsd|;
array[1] = 2|Петров|Петро|sdsd|sdsd|sdfsd|;
String string = "Петров|Петро";
if (!array.contains(string)){
int i = i +1;
array.add(i + "|" + string + string2);}
Answer the question
In order to leave comments, you need to log in
array.contains will only find string references that already exist (full matches).
From the statement of the problem it follows that it is necessary to find a substring.
To do this, you need to use String.contains in a loop, or in a Stream:
if (array.stream().filter(s -> s.contains(string)).count() > 0)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question