java java字符串数组特定单词打印出来
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14920580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java string array specific word print out
提问by user1766709
New to java and trying to figure out how to print out a specific word in a string array. E.g.
Java 新手并试图弄清楚如何打印出字符串数组中的特定单词。例如
String[] myArray = {"bread", "milk", "sugar", "coffee"}
I want to just print out the second value of the array (we say milk
, I know the array index starts at 0
but just for this example we will go with this). Any ideas how to do this. I tried the for loop but cant seem to get it going so if you guys have an example it would be appreciated.
我只想打印出数组的第二个值(我们说milk
,我知道数组索引从 开始,0
但仅在本示例中,我们将使用此值)。任何想法如何做到这一点。我尝试了 for 循环,但似乎无法让它继续,所以如果你们有一个例子,我们将不胜感激。
I cant exactly just print out by using index number. I will give a more detailed approach on how i would like it to work... Say I have two arrays String[] Array1 = {"bread", "milk", "sugar", "coffee"} String[] Array2 = {"butter", "tea", "spoon", "cup"} So if I prompted any entry from array 1 e.g. bread (I would like it to print out something like butter then) so for each value in array1 i would like it to return the value at the same index in array2.
我不能完全使用索引号打印出来。我将给出一个关于我希望它如何工作的更详细的方法......假设我有两个数组 String[] Array1 = {"bread", "milk", "sugar", "coffee"} String[] Array2 = {"butter", "tea", "spoon", "cup"} 所以如果我从数组 1 中提示任何条目,例如面包(我希望它打印出类似黄油的东西)所以对于数组 1 中的每个值,我想要它返回 array2 中相同索引处的值。
回答by Vaishak Suresh
String[] myArray = {"bread", "milk", "sugar", "coffee"}
for(int i=0;i<myArray.length;i++){
if(myArray[i].equals("milk")){
System.out.println(myArray[i]); //Matching the string and printing.
}
}
System.out.println(myArray[1]); //printing the 2nd element if you don't care about the value
回答by Fahim Parkar
Just use
只需使用
System.out.println(myArray[1]);
Demo at www.ideone.com
在 www.ideone.com 上进行演示
If you want to compare, use
如果要比较,请使用
if (myArray[1].equals("milk")) {
// your code
}
If you want to compare in for loop, use below
如果要在 for 循环中进行比较,请在下面使用
String[] myArray = {"bread", "milk", "sugar", "coffee"};
for (int i=0;i<myArray.length;i++) {
if (myArray[i].equals("milk")) {
// your code here....
}
}
Demo for forloop
forloop 的演示
回答by JB Nizet
If you just need to print one element, you don't need any loop:
如果您只需要打印一个元素,则不需要任何循环:
System.out.println(myArray[1]); // prints milk, since indices start at 0
Read the Java tutorial about arrays(or any Java introductory book).
回答by Abubakkar
You can access any element of array directly using an index. You don't require a loop for it.
您可以使用索引直接访问数组的任何元素。你不需要它的循环。
For example, if you want to access 2nd element then you have to write:
例如,如果要访问第二个元素,则必须编写:
yourArray[1]
// remember for accessing using index
// always use (index - 1) in this case for 2nd element (2-1)