Print all arraylist items in one row if they are not empty Java

huangapple go评论79阅读模式
英文:

Print all arraylist items in one row if they are not empty Java

问题

ArrayList items = new ArrayList(Arrays.asList(input.split(";")));

StringBuilder result = new StringBuilder();
for (Object item : items) {
    result.append(item);
}

System.out.println(result.toString());
英文:

I have an Arraylist (items) and to this arraylist I pass (add) rows that I read from a text file (input = rows from file). The input looks like this "0500123;0501023;050100;
0501023;050100;
etc.
Those lines are added into the arraylist (items) by splitting each line with the actual the delimiter(;).

Lines size (arraylist size) varies from 2 to 6.
What I need is to print all arraylist elements in one row only (meaning all lines from the input still to stay in one row). Looping with for, either returns all elements in a new line or all elements of all arrays (all lines) in a single row.

I created the below code with those "if"s. The code is working, and is printing correctly the results but how can I do it shorter? I tried using a Streams but I couldn't achieve that.

How can I do the following, maybe with a loop?

Edit: Thank you are all amazing.

ArrayList items= new ArrayList(Arrays.asList(input.split(";")));
     
if(items.size()==2) System.out.println(items.get(0)+""+items.get(1));
if(items.size()==3) System.out.println(items.get(0)+""+items.get(1)+""+items.get(2));
if(items.size()==4) System.out.println(items.get(0)+""+items.get(1)+""+items.get(2)+""+items.get(3));
if(items.size()==5) System.out.println(items.get(0)+""+items.get(1)+""+items.get(2)+""+items.get(3)+""+items.get(4));
if(items.size()==6) System.out.println(items.get(0)+""+items.get(1)+""+items.get(2)+""+items.get(3)+""+items.get(4)+""+items.get(5));
       }

答案1

得分: 1

你可以基于索引提取一个子列表,并形成输出字符串。

List<String> items = List.of("0500123;0501023;050100".split(";"));
int printFirstN = 2;

String c = String.join(" ", items.subList(0, printFirstN));
System.out.println(c);

输出:0500123 0501023

英文:

You can extract a sublist based on indices and form the output string.

List&lt;String&gt; items = List.of(&quot;0500123;0501023;050100&quot;.split(&quot;;&quot;));
int printFirstN = 2;

String c = String.join(&quot; &quot;, items.subList(0, printFirstN));
System.out.println(c);

Output: 0500123 0501023

答案2

得分: 1

可以将以下代码部分
```java
if(items.size()==2) System.out.println(items.get(0)+&quot;&quot;+items.get(1));
if(items.size()==3) System.out.println(items.get(0)+&quot;&quot;+items.get(1)+&quot;&quot;+items.get(2));
if(items.size()==4) System.out.println(items.get(0)+&quot;&quot;+items.get(1)+&quot;&quot;+items.get(2)+&quot;&quot;+items.get(3));
if(items.size()==5) System.out.println(items.get(0)+&quot;&quot;+items.get(1)+&quot;&quot;+items.get(2)+&quot;&quot;+items.get(3)+&quot;&quot;+items.get(4));
if(items.size()==6) System.out.println(items.get(0)+&quot;&quot;+items.get(1)+&quot;&quot;+items.get(2)+&quot;&quot;+items.get(3)+&quot;&quot;+items.get(4)+&quot;&quot;+items.get(5));

用以下循环方式替代:

String output = &quot;&quot;;
for (String s : items) output += s;
System.out.println(output);

或者

for (String s : items)
    System.out.print(s);

或者

for (int i=0; i&lt;items.size(); i++)
    System.out.print(items.get(i));

<details>
<summary>英文:</summary>

You can replace
```java
if(items.size()==2) System.out.println(items.get(0)+&quot;&quot;+items.get(1));
if(items.size()==3) System.out.println(items.get(0)+&quot;&quot;+items.get(1)+&quot;&quot;+items.get(2));
if(items.size()==4) System.out.println(items.get(0)+&quot;&quot;+items.get(1)+&quot;&quot;+items.get(2)+&quot;&quot;+items.get(3));
if(items.size()==5) System.out.println(items.get(0)+&quot;&quot;+items.get(1)+&quot;&quot;+items.get(2)+&quot;&quot;+items.get(3)+&quot;&quot;+items.get(4));
if(items.size()==6) System.out.println(items.get(0)+&quot;&quot;+items.get(1)+&quot;&quot;+items.get(2)+&quot;&quot;+items.get(3)+&quot;&quot;+items.get(4)+&quot;&quot;+items.get(5));

with a loop in the following ways:

String output = &quot;&quot;;
for (String s : items) output += s;
System.out.println(output);

or

for (String s : items)
    System.out.print(s);

or

for (int i=0; i&lt;items.size(); i++)
    System.out.print(items.get(i));

答案3

得分: 1

public static String extractNumber(String str) {
    return Arrays.stream(str.split("\\s*;\\s*"))    // \s* - just to exclude spaced
                 .filter(num -> !num.isBlank())
                 .collect(Collectors.joining());
}

Output:

String str = "0500123;0501023;050100;  ; 6666;   ";
System.out.println(extractNumber(str));

050012305010230501006666
英文:
public static String extractNumber(String str) {
    return Arrays.stream(str.split(&quot;\\s*;\\s*&quot;))    // \s* - just to exclude spaced
                 .filter(num -&gt; !num.isBlank())
                 .collect(Collectors.joining());
}

Output:

String str = &quot;0500123;0501023;050100;  ; 6666;   &quot;;
System.out.println(extractNumber(str));

050012305010230501006666

答案4

得分: 0

以下是您描述的示例代码的翻译:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String input =
                "1;2\n" +
                "1;2;3;4;5\n" +
                "1;2;3\n" +
                "1\n" +
                "1;2;3;4;5;6";
        Scanner scn = new Scanner(input);
        
        List<String> lines = new ArrayList<>();
        while (scn.hasNextLine()) {
            lines.add(scn.nextLine());
        }
        
        for (String line : lines) {
            String[] data = line.split(";");
            for (String s : data) {
                
                // 打印每个分割数据和一个空格,无换行符:
                System.out.print(s + " ");
            }
            
            // 打印一个换行:
            System.out.println();
        }   
    }
}

以下是程序的输出:

1 2
1 2 3 4 5
1 2 3
1
1 2 3 4 5 6
英文:

Here's some sample code that does what you describe:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		String input =
				&quot;1;2\n&quot; +
				&quot;1;2;3;4;5\n&quot; +
				&quot;1;2;3\n&quot; +
				&quot;1\n&quot; +
				&quot;1;2;3;4;5;6&quot;;
		Scanner scn = new Scanner(input);
		
		List&lt;String&gt; lines = new ArrayList&lt;&gt;();
		while (scn.hasNextLine()) {
			lines.add(scn.nextLine());
		}
		
		for (String line : lines) {
			String[] data = line.split(&quot;;&quot;);
			for (String s : data) {
				
				// Print each split data and a space with no newlines:
				System.out.print(s + &quot; &quot;);
			}
			
			// Print a line break:
			System.out.println();
		}	
	}
}

Here's the output of the program:

1 2
1 2 3 4 5
1 2 3
1
1 2 3 4 5 6

huangapple
  • 本文由 发表于 2020年9月6日 17:44:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63762760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定