用java写在二维字符串中搜索字符并打印该行字符串
时间: 2023-05-22 15:04:57 浏览: 41
Sure, I can help with that. Here's some Java code to search for a character in a 2D string array and print the corresponding row:
```java
public static void searchAndPrint(String[][] arr, char c) {
for (String[] row : arr) {
for (char ch : row[0].toCharArray()) {
if (ch == c) {
System.out.println(row[0]);
break;
}
}
}
}
```
Call this function with a 2D string array and a character to search for, and it will print all rows that contain that character.
For example, if your 2D string array looks like this:
```java
String[][] arr = {{"apple", "banana", "cherry"},
{"date", "fig", "grape"},
{"kiwi", "lemon", "mango"}};
```
And you call the function like this:
```java
searchAndPrint(arr, 'a');
```
It will output:
```
apple
banana
mango
```
I hope that helps!