简体   繁体   English

每次到达char时在下一行打印

[英]Printing on next line every time char is reached

In my code I first add two string arrays to a String arrayList. 在我的代码中,我首先将两个字符串数组添加到String arrayList中。 In my toString() method I want to print the arrayList so that every time char ',' is reached, we print the next array, on the next line (in a row). 在我的toString()方法中,我想打印arrayList以便每次到达char','时,在下一行(一行)中打印下一个数组。 but with this code I get a row of the first array and a column of the second. 但是使用此代码,我得到了第一个数组的行和第二个数组的列。 what am I doing wrong here? 我在这里做错了什么?

点击这里输出

Code : 代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class PrintList {
    static String[] data = {"cat","dog"," ","hen",","};
    static String[] data2 = {"ttt","yyy","ggg","ddd", ","};
    static ArrayList <String> list = new ArrayList<>();
    public static void main(String[] args) {
        dataRecord();
        System.out.print(listToString(list));
    }

    public static void dataRecord () {
        list.addAll(Arrays.asList(data));
        list.addAll(Arrays.asList(data2));

    }
    public static String listToString (List <?> list) {
        String result = "";
        for (int i = 0; i < list.size(); i++) {
           result += "      " + list.get(i);
           if (result.indexOf(',') >= 0) {
              result = result + System.lineSeparator();
           }
        }
        return result;
    } 
}

Just change this if: 只需在以下情况下更改此设置:

if (result.indexOf(',') >= 0) 

for this one: 为此:

if (list.get(i).indexOf(',') >= 0) 

the problem is that you are always looking in the result variable. 问题是您总是在寻找结果变量。 When the first ',' appears the result will always contains the ','. 当第一个','出现时,结果将始终包含','。

That's some strange code there but I assume it's a learning excercise so it's fine I guess. 那是一些奇怪的代码,但我认为这是一种学习练习,所以我猜很好。 Your problem is that once your result string contains a comma you add the line separator each time you add a new element. 您的问题是,一旦结果字符串包含逗号,则每次添加新元素时都添加行分隔符。

Example: assume your list contains "a", "b", ",", "c", "d" . 示例:假设您的列表包含"a", "b", ",", "c", "d"

Now you iterate and add the elements to a string which then looks like this: 现在,您将元素迭代并将其添加到字符串中,该字符串如下所示:

  • you add "a" , the string looks like "a" 您添加"a" ,字符串看起来像"a"
  • you add "b" , the string looks like "ab" 您添加"b" ,字符串看起来像"ab"
  • you add "," , the string looks like "ab ," , now for the first time your string contains a comma so you add the line separator and thus the string looks like "ab ,\\n" 您添加"," ,字符串看起来像"ab ," ,现在您的字符串第一次包含逗号,因此您添加了行分隔符,因此字符串看起来像"ab ,\\n"
  • you add "c" , the string looks like "ab ,\\nc" . 您添加"c" ,字符串看起来像"ab ,\\nc" Because there's still a comma in your string another line separtor is added and the string is "ab ,\\nc\\n" . 因为您的字符串中仍然有逗号,所以添加了另一行分隔符,并且字符串为"ab ,\\nc\\n"
  • you add "d" , the string looks like "ab ,\\nc\\nd" . 您添加"d" ,字符串看起来像"ab ,\\nc\\nd" Because there's still a comma in your string another line separtor is added and the string is "ab ,\\nc\\nd\\n" . 因为您的字符串中仍然有逗号,所以添加了另一行分隔符,并且字符串为"ab ,\\nc\\nd\\n"

To fix that you have several possibilities: 要解决此问题,您有几种可能:

  • check whether the element you just added is a comma or contains one. 检查您刚刚添加的元素是逗号还是包含逗号。 If so add the line separator (and maybe don't add the element) 如果是这样,请添加行分隔符(也许不添加元素)
  • add the elements without any check and replace the commas at the end (after your loop), eg via result = result.replace(",", ",\\n"); 不加检查地添加元素,并在末尾(循环后)替换逗号,例如,通过result = result.replace(",", ",\\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM