[英]How would you print out the character of that string at the index of the string for a given array of Strings
Sorry my question is kind of hard to understand. 抱歉,我的问题有点难以理解。 So in a array such as String []arr = {"abs, "def", "ghi"}; I want to know how it can print out this aei 因此,在诸如String [] arr = {“ abs,” def“,” ghi“}等数组中;我想知道它如何打印出此aei
this is my code that i have tried to write but failed. 这是我尝试编写但失败的代码。
public static void printCharsAt(String[] arr) {
System.out.println("how many inputs do you want in your array");
String array[] = {"hello", "Ap", "CS"};
System.out.println("What term of the array do you want, do you wan the first, 2nd, or third");
tnt char = kb.nextInt();
System.out.println(array[char]);
}
Without any bounds checking: 没有任何界限检查:
for (int i=0; i<arr.length; i++)
System.out.println(arr[i].charAt(i));
You have to loop through each String in the array. 您必须遍历数组中的每个String。 You may use for each
loop in Java or the traditional for
loop. 您可以使用Java中的for each
循环或传统的for
循环。 Once you get a String, You can use the charAt()
method of String
class to obtain the character at a particular position. 一旦获得字符串,就可以使用String
类的charAt()
方法来获取特定位置的字符。 Don't forget the length
method also, It may come in handy. 也不要忘记length
方法,它可能派上用场。 No code, since it is homework :) 没有代码,因为这是家庭作业:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.