有没有办法从每行的数组中打印出不同数量的元素

我希望能够在每一行从我的数组中打印出不同数量的元素。


 String[] name = {"Alix", "Jack", "Alexis", "Adam", "John", "Lexi", 

             "George", "Gregory", "Paul", "Bjorn", "Aaron", "Joseph", "Anderson"};

         System.out.println(Arrays.toString(name));

这个的输出会给我


[Alix, Jack, Alexis, Adam, John, Lexi, George, Gregory, Paul, Bjorn, Aaron, Joseph, Anderson]

但我希望输出类似于以下内容


[Alix, Jack, Alexis]


[Adam, John, Lexi]


[George, Gregory]


[Paul, Bjorn, Aaron, Joseph, Anderson]

有没有办法做到这一点?在我的示例中,每行中的元素数量并不是一成不变的,重要的是我可以改变输出中每行中出现的名称数量。谢谢


尚方宝剑之说
浏览 155回答 3
3回答

慕丝7291255

这是一个示例,可让您为每个列表提供大小列表,因此您可以轻松生成您指定的输出,而不必在每个列表中包含 3 个名称:package com.inlet.ifserver;import java.util.Arrays;public class x {&nbsp; &nbsp; public static void main(String ...args) {&nbsp; &nbsp; &nbsp; &nbsp; String[] name = {"Alix", "Jack", "Alexis", "Adam", "John", "Lexi",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "George", "Gregory", "Paul", "Bjorn", "Aaron", "Joseph", "Anderson"};&nbsp; &nbsp; &nbsp; &nbsp; int[] counts = { 3, 3, 2 };&nbsp; &nbsp; &nbsp; &nbsp; int start = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int count : counts) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int end = Integer.min(start + count, name.length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (end == start)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(Arrays.copyOfRange(name, start, end)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start = end;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (start < name.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(Arrays.toString(Arrays.copyOfRange(name, start, name.length)));&nbsp; &nbsp; }}输出:[Alix, Jack, Alexis][Adam, John, Lexi][George, Gregory][Paul, Bjorn, Aaron, Joseph, Anderson]

慕妹3242003

Arrays.copyOfRange的那样是去这里的方式。此示例提供了一个不同块长度的数组,因此它与您问题中的预期输出相匹配。看起来像这样public static void printInBlocks(String[] arr, int[] blocks) {&nbsp; &nbsp; int idx = 0;&nbsp; &nbsp; for (int blockSize: blocks) {&nbsp; &nbsp; &nbsp; &nbsp; String str = Arrays.toString(Arrays.copyOfRange(name, idx, idx += blockSize));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(str);&nbsp; &nbsp; }}然后你可以这样称呼Main它public static void main(String[] args) {&nbsp; &nbsp; String[] name = {"Alix", "Jack", "Alexis", "Adam", "John", "Lexi",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "George", "Gregory", "Paul", "Bjorn", "Aaron", "Joseph", "Anderson"};&nbsp; &nbsp; int[] blocks = {3, 3, 2, 5};&nbsp; &nbsp; printInBlocks(name, blocks);}输出看起来像[Alix, Jack, Alexis][Adam, John, Lexi][George, Gregory][Paul, Bjorn, Aaron, Joseph, Anderson]笔记其中blocks包含每行项目数的总和必须等于项目数name

子衿沉夜

您可以手动拆分阵列并使用该copyOfRange方法。import java.util.Arrays;public class HelloWorld {&nbsp; &nbsp; &nbsp;public static void main(String []args){&nbsp; &nbsp; &nbsp; &nbsp; String[] name = {"Alix", "Jack", "Alexis", "Adam", "John", "Lexi",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"George", "Gregory", "Paul", "Bjorn", "Aaron", "Joseph", "Anderson"};&nbsp; &nbsp; &nbsp; &nbsp; int splitArrayForSize = 3;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for(int i=0; i< name.length; i+=splitArrayForSize){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] subArray = Arrays.copyOfRange(name, i,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Math.min(name.length, i + splitArrayForSize));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String subArrayAsString = Arrays.toString(subArray);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(subArrayAsString);&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp;&nbsp; &nbsp; }}输出:[Alix, Jack, Alexis][Adam, John, Lexi][George, Gregory, Paul][Bjorn, Aaron, Joseph][Anderson]我确信有一种更简单、更实用的方法来实现这一点。我会对这种方法感兴趣。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java