orient的个人博客分享 http://blog.sciencenet.cn/u/orient

博文

利用ArrayList建立每行元素个数不同的矩阵

已有 3715 次阅读 2012-9-20 05:29 |系统分类:科研笔记| 矩阵, 元素

build a matrix with different length at each row
ArrayList[] row = new ArrayList[rowNum];
for(int i=0;i<rowNum;i++){
   row[i] = new ArrayList<>();
   row[i].add(a);
   row[i].add(b);
}
Just give an example. This example for each row just "a" and "b". By your talent, you can make variant of it and make each row has different length.
Below is from a good link http://stackoverflow.com/questions/10043209/convert-arraylist-into-2d-array-containing-varying-lengths-of-arrays, which is for converting ArrayList into 2D array containing varying lengths of arrays. Which can be a complementary work for mine.

String
[][] array = new String[arrayList.size()][];
for (int i = 0; i < arrayList.size(); i++) {
   
ArrayList<String> row = arrayList.get(i);
    array
[i] = row.toArray(new String[row.size()]);
}



https://blog.sciencenet.cn/blog-264887-614569.html

上一篇:itouch上免费用instapaper保存网页
下一篇:avi2pdf图形显示不全
收藏 IP: 138.47.55.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-3 18:03

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部