Java中二维矩阵的表示与转换
矩阵在线性代数中有着广泛应用,在科学计算、数据分析处理、线性方程等领域也有着广泛应用,可惜JDK中没有相关的矩阵运算工具包,我自己想实现一个,遗憾的是大学里学的线性代数都忘记完了,书本早已回炉再造了。仅仅实现了点点基础的数据转换层的类实现。现先放到这里,希望有兴趣的博友能继续完善!
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Logger;
/**
* 二维结构转换
* File: MatrixStructure.java
* User: leizhimin
* Date: 2008-3-10 16:29:36
*/
public class MatrixStructure {
private static Logger log = Logger.getLogger(MatrixStructure. class.getName());
/**
* 将结果集二维集合数据转换为XML
*
* @param columns 列名集合
* @param result 二维结果集
* @return 返回格式良好的XML字符串
*/
public static String listToXML(ArrayList columns, ArrayList result) {
StringBuffer sb = new StringBuffer();
sb.append( "\n")
.append( ")
.append( "\"?>\n\n");
sb.append( "\n" );
for (ArrayList al : result) {
sb.append( "\t"
);
for (Iterator it_al = al.iterator(), it_c = columns.iterator(); it_al.hasNext() && it_c.hasNext();) {
String tmp = it_c.next().toString();
sb.append( "\n\t\t<").append(tmp).append( ">").append(it_al.next()).append( "<").append(tmp).append( "/>");
}
sb.append( "\n\t\n");
}
sb.append( "");
return sb.toString();
}
/**
* 将结果集二维数组数据转换为XML
*
* @param columns 列名数组
* @param result 二维结果集数组
* @return 返回格式良好的XML字符串
*/
public static String arrayToXML(Object[] columns, Object[][] result) {
StringBuffer sb = new StringBuffer();
sb.append( "\n")
.append( ")
.append( "\"?>\n\n");
sb.append( "\n" );
for (Object[] al : result) {
sb.append( "\t"
);
for ( int i = 0; i < columns.length - 1; i++) {
sb.append( "\n\t\t<").append(columns[i].toString()).append( ">").append(al[i].toString()).append( "<").append(columns[i].toString()).append( "/>");
}
sb.append( "\n\t\n");
}
sb.append( "");
return sb.toString();
}
/**
* 二维集合转为二维数组
*
* @param result 二维集合
* @return 二维数组
*/
public static Object[][] listToArray(ArrayList result) {
int size = result.size();
Object obj[][] = new Object[size][];
if (size == 0) {
log.info( "集合为空,转换数组失败,将返回null!");
return null;
}
for ( int i = 0; i < size; i++) {
obj[i] = result.get(i).toArray();
}
return obj;
}
/**
* 将二维数组转为二维ArrayList
*
* @param arr 二维数组
* @return 二维ArrayList
*/
public static ArrayList arrayToList(Object[][] arr) {
ArrayList all =
new ArrayList();
for ( int i = 0; i < arr.length; i++) {
all.add(arrayToList(arr[i]));
}
return all;
}
/**
* 将一维数组转为List对象
*
* @param ar 一维数组
* @return List对象
*/
public static ArrayList arrayToList(Object[] ar) {
ArrayList al = new ArrayList();
for (Object obj : ar) {
al.add(obj);
}
return al;
}
/**
* 输出二维集合到控制台
*
* @param all 二维集合
*/
public static void listOutputConsole(ArrayList all) {
System.out.println( "-------输出二维集合到控制台-------");
for (ArrayList al : all) {
for (Object a : al) {
System.out.print(a.toString() + "\t");
}
System.out.println();
}
System.out.println( "\n------------------------");
}
/**
* 将二维数组输出到控制台
*
* @param arr 二维数组
*/
public static void arrrayOutputConsole(Object[][] arr) {
System.out.println( "-------输出二维数组到控制台-------");
for (Object[] ar : arr) {
for (Object a : ar) {
System.out.print(a.toString() + "\t");
}
System.out.println();
}
System.out.println( "\n------------------------");
}
/**
* 将二维数组转为格式美观的字符串
*
* @param arr 二维数组
*/
public static String arrayToString(Object[][] arr) {
StringBuilder sb = new StringBuilder();
for (Object[] ar : arr) {
for (Object a : ar) {
sb.append(a.toString()).append( "\t");
}
sb.append( "\n");
}
return sb.toString();
}
/**
* 将二维集合转为格式美观的字符串
*
* @param all 二维集合
*/
public static String listToString(ArrayList all) {
StringBuilder sb = new StringBuilder();
for (ArrayList al : all) {
for (Object a : al) {
sb.append(a.toString()).append( "\t");
}
sb.append( "\n");
}
return sb.toString();
}
public static void main(String args[]) {
ArrayList all =
new ArrayList();
ArrayList al0 = new ArrayList();
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();
al0.add( "01");
al0.add( "02");
al1.add( "11");
al1.add( "12");
al2.add( "21");
al2.add( "22");
all.add(al0);
all.add(al1);
all.add(al2);
listOutputConsole(all);
StringBuffer sb = new StringBuffer();
ArrayList c = new ArrayList();
c.add( "c1");
c.add( "c2");
c.add( "c3");
Object[][] obj = listToArray(all);
arrrayOutputConsole(obj);
ArrayList allx = arrayToList(obj);
String x = arrayToXML(c.toArray(), obj);
System.out.println(x);
}
}
import java.util.Iterator;
import java.util.logging.Logger;
/**
* 二维结构转换
* File: MatrixStructure.java
* User: leizhimin
* Date: 2008-3-10 16:29:36
*/
public class MatrixStructure {
private static Logger log = Logger.getLogger(MatrixStructure. class.getName());
/**
* 将结果集二维集合数据转换为XML
*
* @param columns 列名集合
* @param result 二维结果集
* @return 返回格式良好的XML字符串
*/
public static String listToXML(ArrayList
StringBuffer sb = new StringBuffer();
sb.append( "\n")
.append( ")
.append( "\"?>\n\n");
sb.append( "
for (ArrayList al : result) {
sb.append( "\t
for (Iterator it_al = al.iterator(), it_c = columns.iterator(); it_al.hasNext() && it_c.hasNext();) {
String tmp = it_c.next().toString();
sb.append( "\n\t\t<").append(tmp).append( ">").append(it_al.next()).append( "<").append(tmp).append( "/>");
}
sb.append( "\n\t\n");
}
sb.append( "");
return sb.toString();
}
/**
* 将结果集二维数组数据转换为XML
*
* @param columns 列名数组
* @param result 二维结果集数组
* @return 返回格式良好的XML字符串
*/
public static String arrayToXML(Object[] columns, Object[][] result) {
StringBuffer sb = new StringBuffer();
sb.append( "\n")
.append( ")
.append( "\"?>\n\n");
sb.append( "
for (Object[] al : result) {
sb.append( "\t
for ( int i = 0; i < columns.length - 1; i++) {
sb.append( "\n\t\t<").append(columns[i].toString()).append( ">").append(al[i].toString()).append( "<").append(columns[i].toString()).append( "/>");
}
sb.append( "\n\t\n");
}
sb.append( "");
return sb.toString();
}
/**
* 二维集合转为二维数组
*
* @param result 二维集合
* @return 二维数组
*/
public static Object[][] listToArray(ArrayList
int size = result.size();
Object obj[][] = new Object[size][];
if (size == 0) {
log.info( "集合为空,转换数组失败,将返回null!");
return null;
}
for ( int i = 0; i < size; i++) {
obj[i] = result.get(i).toArray();
}
return obj;
}
/**
* 将二维数组转为二维ArrayList
*
* @param arr 二维数组
* @return 二维ArrayList
*/
public static ArrayList
ArrayList
for ( int i = 0; i < arr.length; i++) {
all.add(arrayToList(arr[i]));
}
return all;
}
/**
* 将一维数组转为List对象
*
* @param ar 一维数组
* @return List对象
*/
public static ArrayList arrayToList(Object[] ar) {
ArrayList al = new ArrayList();
for (Object obj : ar) {
al.add(obj);
}
return al;
}
/**
* 输出二维集合到控制台
*
* @param all 二维集合
*/
public static void listOutputConsole(ArrayList
System.out.println( "-------输出二维集合到控制台-------");
for (ArrayList al : all) {
for (Object a : al) {
System.out.print(a.toString() + "\t");
}
System.out.println();
}
System.out.println( "\n------------------------");
}
/**
* 将二维数组输出到控制台
*
* @param arr 二维数组
*/
public static void arrrayOutputConsole(Object[][] arr) {
System.out.println( "-------输出二维数组到控制台-------");
for (Object[] ar : arr) {
for (Object a : ar) {
System.out.print(a.toString() + "\t");
}
System.out.println();
}
System.out.println( "\n------------------------");
}
/**
* 将二维数组转为格式美观的字符串
*
* @param arr 二维数组
*/
public static String arrayToString(Object[][] arr) {
StringBuilder sb = new StringBuilder();
for (Object[] ar : arr) {
for (Object a : ar) {
sb.append(a.toString()).append( "\t");
}
sb.append( "\n");
}
return sb.toString();
}
/**
* 将二维集合转为格式美观的字符串
*
* @param all 二维集合
*/
public static String listToString(ArrayList
StringBuilder sb = new StringBuilder();
for (ArrayList al : all) {
for (Object a : al) {
sb.append(a.toString()).append( "\t");
}
sb.append( "\n");
}
return sb.toString();
}
public static void main(String args[]) {
ArrayList
ArrayList al0 = new ArrayList();
ArrayList al1 = new ArrayList();
ArrayList al2 = new ArrayList();
al0.add( "01");
al0.add( "02");
al1.add( "11");
al1.add( "12");
al2.add( "21");
al2.add( "22");
all.add(al0);
all.add(al1);
all.add(al2);
listOutputConsole(all);
StringBuffer sb = new StringBuffer();
ArrayList c = new ArrayList();
c.add( "c1");
c.add( "c2");
c.add( "c3");
Object[][] obj = listToArray(all);
arrrayOutputConsole(obj);
ArrayList
String x = arrayToXML(c.toArray(), obj);
System.out.println(x);
}
}
运行结果:
-------输出二维集合到控制台-------
01 02
11 12
21 22
------------------------
-------输出二维数组到控制台-------
01 02
11 12
21 22
------------------------
01
02
11
12
21
22
Process finished with exit code 0
01 02
11 12
21 22
------------------------
-------输出二维数组到控制台-------
01 02
11 12
21 22
------------------------
Process finished with exit code 0