문자 스트림에서 첫 번째 비반복 문자를 찾으려면 Java 코드는 다음과 같습니다. -
예
import java.util.ArrayList;
import java.util.List;
public class Demo{
final static int max_chars = 256;
static void non_repeating_char(){
List<Character> my_list = new ArrayList<Character>();
boolean[] repeat = new boolean[max_chars];
String my_str = "Thisisasample";
for (int i = 0; i < my_str.length(); i++){
char x = my_str.charAt(i);
if (!repeat[x]){
if (!(my_list.contains(x))){
my_list.add(x);
}
else{
my_list.remove((Character)x);
repeat[x] = true;
}
}
if (my_list.size() != 0){
System.out.print("The first non-repeating character of the string is ");
System.out.println(my_list.get(0));
}
}
}
public static void main(String[] args){
non_repeating_char();
}
} 출력
The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T
Demo라는 클래스에는 'non_repeating_char' 함수라는 함수가 포함되어 있습니다. 목록이 생성되고 문자열이 정의됩니다. 이 문자열은 반복되고 모든 문자가 검사되며 그 개수는 'repeat'라는 이름의 배열에 부울 변수 형식으로 저장됩니다. 값이 반복되면 true이고 그렇지 않으면 false입니다. 메인 함수에서 함수가 호출되고 콘솔에 해당 메시지가 표시됩니다.