Use java to determine the position where a character first appears

王林
Release: 2021-01-15 10:22:54
forward
2916 people have browsed it

Use java to determine the position where a character first appears

Purpose:

(Learning video sharing:java video tutorial)

Use java to determine the position where a character first appears

The implementation code is as follows:

import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String[] a = { "a", "e", "i", "o", "u" }; List list = new LinkedList(); int i; boolean flag = false; for (i = 0; i < a.length; i++) { if (s.indexOf(a[i]) != -1) {//s.indexOf(" ")返回元素的下标 list.add(s.indexOf(a[i]) + 1); flag = true; } } if (flag == false) { System.out.println("0"); } else { Collections.sort(list);//要返回元音字母首次出现的位置,所以先排序 System.out.print(list.get(0));//在输出第一个元素就可以 } sc.close(); } }
Copy after login

The most important thing is

1. Use List collection List list = new LinkedList()

2. Be sure to use s.indexof() Methods!

3. Collections.sort(list) Sort the collection

Related recommendations:java introductory tutorial

The above is the detailed content of Use java to determine the position where a character first appears. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!