java - 如何在java中仅打印出具有5个元素的数组的前3个元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31195004/
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
How do I print out only the first 3 elements of an array that has 5 elements in java?
提问by Max Echendu
How do I only print out the first 5 elements of an array that has 10 elements?
如何仅打印出具有 10 个元素的数组的前 5 个元素?
I know I can just do:
我知道我可以这样做:
String[] strArray = {"bacon", "chicken", "ham", "egg", "poop"};
System.out.println(strArray[0]);
System.out.println(strArray[1]);
System.out.println(strArray[2]);
But assuming I had 100 elements and wanted to print out the first 25 elements. Or wanted to print out all elements from the 10th to the 50th. Writing all the lines of code would take up time.
但是假设我有 100 个元素并且想要打印出前 25 个元素。或者想打印出从第 10 个到第 50 个的所有元素。编写所有代码行会占用时间。
Is there like a way to tell the console to print "from" one element to another? Sort of like from 10-50? So like a short-cut.
有没有办法告诉控制台“从”一个元素打印到另一个元素?有点像 10-50?所以就像一个捷径。
采纳答案by habsq
Print first 25 elements:
打印前 25 个元素:
for (int i=0; i<25; i++) {
System.out.println(strArray[i]);
}
Print element number 10 to 50:
打印元素编号 10 到 50:
for (int j=9; j<50; j++) {
System.out.println(strArray[j]);
}
回答by RudyMenon
You can do this very easily using a for loop
您可以使用 for 循环轻松完成此操作
String[] strArray = {"bacon", "chicken", "ham", "egg", "poop"};
for (i = 0; i < 3; i ++){
Ssytem.out.println(strArray[i]);
};
回答by SSH
Although this is very basics, but just giving a reference code so that you can write a generic method like this one, which takes string array and start and end index from where you need to print , and checking if your starting is not less than 0 or ending greater than size of the array itself..
虽然这是非常基础的,但只是给出了一个参考代码,这样你就可以写一个像这样的通用方法,它从你需要打印的地方获取字符串数组和开始和结束索引,并检查你的开始是否不小于0或结尾大于数组本身的大小..
public void printElements(String[] strArray,int start , int end)
{
if(start > 0 && end <strArray.length)
for (int i=start; i<end; i++)
{
System.out.println(strArray[i]);
}
}
回答by stuck
You can do that with a function like that:
你可以用这样的函数来做到这一点:
public void print(String [] array, int range1, int range2){
for(int i = range1, i < range2, i++)
System.out.println(array[i]);
}
You can add some if controls for range1 and range2 values. For example these values can't be lower than zero or higher than array.length
您可以为 range1 和 range2 值添加一些 if 控件。例如,这些值不能低于零或高于 array.length
回答by ROMANIA_engineer
Since Java 8
从 Java 8 开始
You can use limit
from the Stream
class:
您可以limit
从Stream
类中使用:
Stream.of(strArray).limit(3).forEach(System.out::println);
or
或者
Arrays.stream(strArray).limit(3).forEach(System.out::println);
回答by Ayushkunj
It's very simple to print elements of array. Let's take a function printElemnts()
to demonstrate how to print elements of array.
打印数组元素非常简单。让我们用一个函数printElemnts()
来演示如何打印数组元素。
public void printElements(String [] array, int startingIndex, int lastIndex) {
for (int i = startingIndex; i < lastIndex; i++)
System.out.println(array[i]);
}
In your case you can set the startingIndex
as zero and lastIndex
as some value.
在您的情况下,您可以将 设置startingIndex
为零和lastIndex
某个值。