如何转置List?
我有一个以下 ArrayList,
[Title,Data1,Data2,Data3]
[A,2,3,4]
[B,3,5,7]
我想像这样转换这个,
[Title,A,B]
[Data1,2,3]
[Data2,3,5]
[Data3,4,7]
我对这种方法有点困惑。任何提示将不胜感激。
谢谢。
I have a following ArrayList,
[Title,Data1,Data2,Data3]
[A,2,3,4]
[B,3,5,7]
And I would like to convert this one like this,
[Title,A,B]
[Data1,2,3]
[Data2,3,5]
[Data3,4,7]
I'm bit confused with the approach. Any hint would be much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这称为转置。以下代码段满足您的需要:
另请参阅
This is called transposition. The following snippet does what you need:
See also
这是我的解决方案。感谢@jpaugh 的代码。希望这对您有帮助。^_^
Here is my solution.Thanks to @jpaugh's code.I hope this will help you.^_^
这不是一种快速的方法,也不是最干净的方法,而是更多 kotlin 风格的代码。
第一行创建一个 M 大小的“结果”列表(对于 NxM 原始矩阵),而第二行 =>
Not a fast way, neither the cleanest one, but more of kotlin-ish code.
the first line creates a M size 'result' list (for NxM original matrix), while the second line =>
这称为转置操作。代码示例位于此处,但需要进行重大修改因为你有一个数组的 ArrayList (我从你的问题中推断出)
This called a transpose operation. A code sample is here, , but will need significant modification as you have an ArrayList of Arrays (what I infer from your question)
也许是这样的
something like this maybe
背后的数学原理:您需要转置矩阵。如果使用二维数组或“列表列表”会更容易,这与集合几乎相同。数组列表也可以工作,但它有点令人困惑。
这篇维基百科文章展示了一些转置算法。
The mathematics behind: you need to transpose the matrix. It's easier if you use a 2-dimensional array or a 'List of Lists', which is pretty much he same with collections. A List of arrays works too, but it is slightly more confusing.
This wikipedia article shows some algorithms for transposition.
该技术称为转置。实施示例。
The technique is called transposing. Example of an implementation.
检查所有列表的大小是否相同。
将信息放入矩阵(例如,包含列表元素的列表)中以获取列表的数量和列表的大小。
使用旋转后的尺寸信息创建一个新的矩阵。 (3x4 到 4x3)
实施 2 个 For 循环并将元素放入新矩阵中。
Check whether all lists have the same size.
Place the informations in a Matrix (List with List elements, for example) to get the number of lists and size of a list.
Create a new Matrix with the rotated size informations. (3x4 to 4x3)
Implement 2 For loops and place the elements into the new matrix.
如果是用于数据迁移任务,如果大小不太大,您可以考虑使用友好的电子表格。
对于矩阵操作,jScience 库具有矩阵支持。对于仅仅转置矩阵来说,这有点过分了,但这取决于需要用它做什么。
If it is for a datamigration task, you might consider your friendly spreadsheet if the size is not too big.
For matrix manipulation stuff there is the jScience library which has matrix support. For just transposing a metrix this would be overkill, but it depends what needs to be done with it.
您是否有固定数量的 ArrayList,并且它们一开始就有固定的大小?如果它是固定的,你可以做的是有一个 int 索引值并在同一个循环中依次处理每个 ArrayList。然后,您可以将每个值传输到临时 ArrayList,然后将对此的引用放置在最终 ArrayList 中以进行输出。
听起来很混乱?这是一个粗略的解决方案:
Do you have a fixed number of ArrayLists and are they of fixed size to begin with? If it is fixed, what you can do is have an int index value and process each ArrayList in turn in the same loop. You can then transfer each value to a temporary ArrayList and then place a reference to this in a final ArrayList for output.
Sounds confusing? Here's a rough solution:
由于 get(index) 可能会极大地损害您的性能,即对于大链接列表,我建议使用 @polygenelubricants 解决方案,但使用迭代器方法。
Since
get(index)
could harm your performance dramatically i.e. for big linked list I would recommend using @polygenelubricants solution but with an iterator approach.在 Kotlin 中,简单的转置如下所示:
图例:
getOrNull
处理较短行中缺失的项目。map()
作为List
返回。In Kotlin, a simple transposing would look like:
Legend:
forEachIndexed
iterates over indexes of the first row.getOrNull
handles the missing items in shorter rows.map()
as aList
.