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

Java finds array elements of the specified type

2022-02-02 19:41:12 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




Refer to the answer 1:
public static void main(String[] args) {    Scanner sc = new Scanner(System.in);    String target = "";    int index = -1;    System.out.println(" Please enter the target statement length ");    String length = sc.nextLine();    String[] item = new String[new Integer(length)];    System.out.println(" Please enter the target statement ");    String sentence = sc.nextLine();    item = sentence.split(" ");    for (int i = 0; i < item.length; i++) {        if (item[i].length()>target.length()&&item[i].startsWith("b")) {            target = item[i];            index = i;        }    }    if (index == -1){        System.out.println(index);    }else {        System.out.println(index + " " +target);    }}



Refer to the answer 2:

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

Random recommended