1

I've been trying to get each character from every String by column but I only got the first characters of every string, I want to get every character by column from every string.

For example:

I have three strings from ArrayList of Strings:

  1. chi
  2. llo
  3. ut

What I want to happen must be like this, after getting each character by column from strings:

  1. clu
  2. hlt
  3. io

So long, my current source code only gets the first characters of first two string which is 'cl', Here's my current source code:

List<String> New_Strings = new ArrayList<String>();
int Column_Place = 0;
for (String temp_str : Strings) {
    try{ //For StringIndexOutOfBoundsException (handle last String)
        if(Column_Place >= temp_str.length()){
            Current_Character = temp_str.charAt(Column_Place);
            New_Strings.add(Character.toString(Current_Character));
            break;
        }else if (Column_Place < temp_str.length()){
            Current_Character = temp_str.charAt(Column_Place);
            New_Strings.add(Character.toString(Current_Character));
        }
    }catch(Exception e){
        continue;
    }
    Column_Place++;
}
4

4 回答 4

0

只需调用groupByColumn(Arrays.asList("chi", "llo", "ut")

public static List<String> groupByColumn(List<String> words) {
    if (words == null || words.isEmpty()) {
        return Collections.emptyList();
    }

    return IntStream.range(0, longestWordLength(words))
            .mapToObj(ind -> extractColumn(words, ind))
            .collect(toList());

}

public static String extractColumn(List<String> words, int columnInd) {
    return words.stream()
            .filter(word -> word.length() > columnInd)
            .map(word -> String.valueOf(word.charAt(columnInd)))
            .collect(Collectors.joining(""));
}

public static int longestWordLength(List<String> words) {
    String longestWord = Collections.max(words, Comparator.comparing(String::length));
    return longestWord.length();
}
于 2018-12-22T10:49:01.007 回答
0

您正在将单个字符的字符串表示形式添加到结果字符串中。相反,您应该将这些字符累积到结果字符串中。例如:

int numStrings = strings.size();
List<String> result = new ArrayList<>(numStrings);
for (int i = 0; i < numStrings; ++i) {
    StringBuilder sb = new StringBuilder();
    for (String s : strings) {
        if (i < s.length) {
            sb.append(s.charAt(i));
        }
    }
    result.add(sb.toString());
}
于 2018-12-22T10:52:28.820 回答
0

只需将列表视为二维数组。从列表中拆分每个项目,从每个项目中获取第 j 个字符,当且仅当项目的长度大于索引 j。

    ArrayList<String> list = new ArrayList<String>();
    list.add("chi");
    list.add("llo");
    list.add("ut");


    int size = list.size();
    int i=0, j=0,k=0;
    while(size-- > 0){
        for(i=0; i<list.size(); i++){
            String temp = list.get(i);
            if(j < temp.length()){
                System.out.print(temp.charAt(j));
            }
        }
        j++;
        System.out.println();
    }
于 2018-12-22T19:35:20.530 回答
0

您使用增强的/foreach 循环对 List 进行迭代。因此,您将对每个字符串进行一次迭代。而你的结果:只处理第一个字母。
您应该通过这种方法使用带有 as条件的while循环。 或者作为替代方案,您可以分两个不同的步骤执行操作并使用 Java 8 功能。 whilewhile(Column_Place < Strings.size())

请注意,在 Java 中,变量以小写字母开头。请遵循约定,以使您的代码在此处和那里更具可读性/可理解性。

在 Java 8 中,你可以这样做:

List<String> strings = new ArrayList<>(Arrays.asList("chi", "llo", "ut"));

int maxColumn = strings.stream()
                 .mapToInt(String::length)
                 .max()
                 .getAsInt(); // suppose that you have at least one element in the List


List<String> values =
        // stream from 0 the max number of column
        IntStream.range(0, maxColumn) 
                 // for each column index : create the string by joining their 
                 // String value or "" if index out of bound
                 .mapToObj(i -> strings.stream() 
                                       .map(s -> i < s.length() ? String.valueOf(
                                               s.charAt(i)) : "")
                                       .collect(Collectors.joining()))
                 .collect(Collectors.toList());
于 2018-12-22T10:32:52.467 回答