首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图打印转置string[]

试图打印转置string[]
EN

Stack Overflow用户
提问于 2016-10-18 22:11:19
回答 3查看 552关注 0票数 0

所以我一直想要一个像这样输入的txt文件-

代码语言:javascript
复制
abcddhdj
efghdd
ijkl

为了得到这个-

代码语言:javascript
复制
j
d
hd
dd
dhl
cgk
bfj
aei

我尝试使用2d char数组来完成这一任务,该数组给出了空异常和arrayoutofbound错误,但大部分没有工作,然后尝试使用字符串数组、数组数组,最后,我尝试使用字符串的数组。

这是我在使用string[]进行了大量搜索后得到的最接近我的解决方案-

代码语言:javascript
复制
public static void main(String[] args) throws IOException
{
    BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt"));         // PUT YOUR FILE LOCATION HERE
    int k=0,i,j=0,x;
    String line[] = new String[10] ;     //SET THE APPROXIMATE NUMBER OF ROWS
    while((line[k] = br.readLine()) !=null)
        {System.out.println(line[k]);      //print to check input - verified
         k++;


        }
    for(x=0;x<k;x++)
    {if(j<line[x].length())
       {j=line[x].length()-1;}    //this part not working in above loop
    }
    System.out.println(j);    // verified but not working inside previous loop for some reason
    System.out.println(k);


    for(x=j-1;x>=0;x++)         //without this loop,its perfect, but with it gives  indexoutofbound error , doesnt run at x=j
    {  for(i=0;i<k;i++)
       { System.out.print(line[i].charAt(x));
       } 
       System.out.println();
    }

}

这是一个输出

代码语言:javascript
复制
run:
abcd
efgh
ijkl
4     //should have come as 3 since i did length-1
3
chl     //notice the d missing , every char of first row shifted,just why
bgk    //in outofbound error , it only prints d at the end, need explanation 
afj
ei
BUILD SUCCESSFUL (total time: 0 seconds)

如果我在abcd之后加了一个空格,它给出了索引和k之后的输出,最后我使用了另一种方法,即增加空格使所有长度相等,但是输出仍然是错误的,而且这种想法有问题,应该有更好的方法。

所以我试了一下,这又给了我更多的问题

试着用任何可以理解的方法解决这个问题。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-10-18 22:37:33

这应该能起作用:

这里的关键是,我用空字符填充所有的行数组,这样每个字符数组的长度都与最长的行相同。

代码语言:javascript
复制
public static void main(String[] args)
{
    try (BufferedReader br = new BufferedReader(new FileReader("C:\\test.txt")))
    {
        String line;
        List<List<Character>> lines = new ArrayList<>();

        int longestLine = 0;
        while((line = br.readLine()) !=null)
        {
            line = line.trim();

            if (line.length() > 0)
            {
                List<Character> currList = new ArrayList<>();
                for (char c : line.toCharArray())
                {
                    currList.add(c);
                }

                if (currList.size() > longestLine)
                {
                    longestLine = currList.size();
                }

                lines.add(currList);
            }
        }

        // pad all lists to be the same as the longest
        for (List<Character> currList : lines)
        {
            while (currList.size() < longestLine)
            {
                currList.add(Character.MIN_VALUE);
            }
        }

        // go through each list backwards
        for (int i = longestLine - 1; i >= 0; i-- )
        {
            for (List<Character> currList : lines)
            {
                System.out.print(currList.get(i));
            }
            System.out.println();
        }
    }
    catch (Throwable t)
    {
        t.printStackTrace();
    }
}

示例输入

代码语言:javascript
复制
abcd
efgh
ijkl
g

示例输出

代码语言:javascript
复制
dhl
cgk
bfj
aeig
票数 1
EN

Stack Overflow用户

发布于 2016-10-18 22:41:18

假设输入被读取到数组中

代码语言:javascript
复制
ArrayList<String> inputList = new ArrayList<String>();
inputList.add("abcddhdj");
inputList.add("efghdd");
inputList.add("ijkl");

int maxSize = 0;
for (String input : inputList) {
    if (input.length() > maxSize) {
        maxSize = input.length();
    }
}
String outputList[] = new String[maxSize];
for (int i = 0; i < maxSize; i++) {
    String output = "";
    for (String input : inputList) {
      if(i<input.length())
          output=output+input.charAt(i);
    }
    outputList[maxSize-(i+1)]=output;
}
票数 0
EN

Stack Overflow用户

发布于 2016-10-18 23:54:55

将全部存储到2d数组并转置到打印循环中。

代码语言:javascript
复制
    final char[][] matrix = Files.lines(Paths.get(fileName)).map(String::toCharArray).toArray(char[][]::new);
    final int width = Arrays.stream(matrix).mapToInt(a -> a.length).max().getAsInt();

    for (int i = 0; i < width; ++i ) {
        final int idx = width-i-1;
        String s = Arrays.stream(matrix).map(a -> a.length > idx ? String.valueOf(a[idx]) : " ").collect(Collectors.joining());
        System.out.println(s);
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40118945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档