current position:Home>Java finds array elements of the specified type

Java finds array elements of the specified type

2022-02-02 19:47:28 CSDN Q & A

Title Description
Enter an integer n, Then input n A string , Will this n A string is saved in the array . Find the longest string in the above string and start with b Starting string , Output the subscript of this string in the array in a separate line 、 The value of the string itself , Separate... With spaces in the middle . If there are multiple elements that meet the requirements , Just output the relevant content of the element with the smallest subscript . If not with b Starting string , The output -1.

sample input
5
wonderful beauty peace bit am

sample output
1 beauty
The following is the code I learned before to find the smallest string in dictionary order in array elements , I hope it can be modified on this basis

```javaimport java.util.Scanner;public class Main{    public static void main(String[] args) {        int min_index = 0;        Scanner n_scanner = new Scanner(System.in);        int n = n_scanner.nextInt();        Scanner str_scanner = new Scanner(System.in);        String[] str_arr = new String[n];                 for(int i = 0; i < n; i++) {            str_arr[i] = str_scanner.next();        }        for (int i = 0; i < str_arr.length - 1; i++) {            if(str_arr[0].compareTo(str_arr[i + 1]) >= 0){                str_arr[0] = str_arr[i + 1];                min_index = i + 1;            }        }        System.out.println(min_index + " " + str_arr[0]);    }}

```




Refer to the answer 1:

img




Refer to the answer 2:



Refer to the answer 3:
public class Main {    public static void main(String[] args) {        Main main = new Main();        int min_index = -1;// Initial value -1        Scanner n_scanner = new Scanner(System.in);        int n = n_scanner.nextInt();        Scanner str_scanner = new Scanner(System.in);        String[] str_arr = new String[n];        for(int i = 0; i < n; i++) {            str_arr[i] = str_scanner.next();        }        // This problem requires 1.b head  2. The longest         for (int i = 0; i < str_arr.length; i++) {            // It's reversed here , Want the longest , And b start , Equivalent to :            // 1.b The beginning is necessary             // 2. One of the two conditions not found before or longer than before             // 3. Write the wrong , Length comparison is not dictionary order             if((min_index == -1 || str_arr[min_index].length()<str_arr[i].length() ) && (str_arr[i].startsWith("b"))){                // Don't say that , The original data has been changed, which is not conducive to subsequent query bug, It's also easier to make mistakes // str_arr[0] = str_arr[i + 1];                min_index = i;            }        }        // Change to min_index        if(min_index == -1){            System.out.println(min_index);        }else            System.out.println(min_index + " " + str_arr[min_index]);    }}



Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202021947253357.html

Random recommended