我有一个项目,它使用链接列表和数组来创建数学中常见的矩阵和向量。我想知道如何获取linkedlist节点的值并将它们存储在一个数组中。该项目要求从链表Matrix(SparseMatixLinkedList)中移动元素,因为矩阵是通过数组的构造形成的,并将其值移动到数组中,使其成为通过数组构造(DenseMatrix)的矩阵。
如果我想将数组矩阵中的元素移回链表矩阵,有人会知道如何做到这一点,反之亦然吗? (SparseMatrix和DenseMatrix方法低于评论短语'for app 5'。)
import java.util.*;
interface Matrix
{
Vector VecArray[][] = new Vector[]{
// Assign the value x to element i,j
public void set(int i,int j, double x){
for(int i =0; i < data.length; i++){
data[i] = x;
}
}
// i = data [i][i];
// get the value of the element i,j
double get(int i, int j);
// Extract the diagonal of the matrix
double[] getDiagonal();
// get the size of the matrix-- number of rows
public int getSize(){
this.size = size;
return size;
}
int count = 0;
// for (RowNode r = head; r != null; r = r.next){
// count++;
// }
return count;
}
// get the number of non-zero elements
int getNnz(){
int i;
int j;
int [][] aElements = new int [i][j];
for (int k = 0 ; k < aElements.length; k++){
int x = aElements.length;
int y = aElements[0].length;
int result = x * y;
return result;
}
}
// Multiply a matrix by a vector
Vector multiply(Vector B);
public static int[][] multMatrix(int a[][], Vector B){//a[m][n], b[n][p]
if(a.length == 0) return new int[0][0];
if(a[0].length != B.length) return null; //invalid dims
int n = a[0].length;
int m = a.length;
int p = B.getSize();
// int ans[][] = new int[m][p];
double[] newV = new double [B.getSize()];
int value;
for(int i = 0; i < n; i++){
value = 0;
for(int j = 0; j < m; j++){
value += a[i][j] * B.get(i);
}
newV[i] = value;
}
// for(int i = 0;i < m;i++){
// for(int j = 0;j < p;j++){
// for(int k = 0;k < n;k++){
// ans[i][j] += a[i][k] * B[k][j];
// }
// }
// }
// return ans;
// }
// }
// Vector A;
// Vector B;
// for(int i = 0; i < VecArray.length; i++){
// for(int j = 0; j < VecArray.length; j++){
//
// Vector k = A * B;
// }
// }
// Print matrix using a specific format
public void display();
for (int row = 0; row < x.length; row++) {
for (int column = 0; column < x[row].length; column++) {
System.out.print(x[row][column] + "\t");
}
System.out.println();
System.out.println("This is the first array");
display(firstarray);
System.out.println("This is the second array");
display(secondarray);
// return info about the matrix
void info();
//List<Double> SparseMatrixLinkedList = new LinkedList<Double>(Arrays.asList(1.0, 2.0));
}
//////////////////////////////////// ARRAY DENSE MATRIX IMPLEMENTATION
class DenseMatrix implements Matrix
{
//////// TO COMPLETE--you can uncomment the instructions below
private int size=0; // size of the matrix- number of rows/columns
private int nnz=0; // number of non-zero elements
private double[][] data;
public DenseMatrix(int n) {
// TODO Auto-generated constructor stub
}
/////// return info about the matrix
public void info(){
System.out.println("Dense Matrix n="+size+", nnz="+nnz+", Storage="+(8*size*size)+"b or "+(8*size*size)/(1024*1024)+"Mb");
}
}
//////////////////////////////////// Linked-List SPARSE MATRIX IMPLEMENTATION
//public RowNode rArray[] = new RowNode (100){
//
//public ColNode cArray[] = new ColNode (100){
//
//}
data[][] = new Object [RowNode] [ColNode];
class RowNode{
public int rowindex;
public ColNode col;
public RowNode next;
RowNode(int i){rowindex=i; col=null; next=null;}
}
class ColNode{
public double entry;
public int colindex;;
public ColNode next;
ColNode(int j,double x){
colindex=j;
entry=x;
next=null;}
}
class SparseMatrixLinkedList implements Matrix
{
List<RowNode> r1 = new LinkedList<RowNode>();
Node c1 = new LinkedList <ColNode>();
// int l;
// if(Link l : r1){
// l = new LinkedList<ColNode>(1,2);
// }
private RowNode top;
private int size=0;
private int nnz=0;
// constructors
// Basic constructor- no element in the list yet
SparseMatrixLinkedList(DenseMatrix, size, nnz){
top=null;
first = newLink;
}
}
//for App 5:
SparseMatrixLinkedList(DenseMatrix, size, nnz){
LinkedList<SparseMatrixLinkedList> mlinkedlist = new LinkedList<SparseMatrixLinkedList>();
// (DenseMatrix, size, nnz){
// top=null;
// first = newLink;
for(int i = 0; i < size; i++){
mlinkedlist.add(data[i]);
}
DenseMatrix(SparseMatrixLinkedList){
mArray[] = new DenseMatrix [size];
for(int i = 0; i < size; i++){
mArray[i] = new link;
}
}
Linkedlist S&EMatrix = new LinkedList <Matrix>(nnz, size){
}
// methods
public void vectorArray(){
}
public void display(){
RowNode current = top; //start probe at the beginning
System.out.println("i");
while (current!=null) { // until the end of the list
System.out.print(current.rowindex+" ");
ColNode jcurrent = current.col;
while (jcurrent!=null) { // until the end of the list
System.out.format("==> (j=%d, a=%.4f)",jcurrent.colindex,jcurrent.entry);
jcurrent = jcurrent.next;
}
System.out.println();
current = current.next; // move to next Link
}
System.out.println();
}
// return info about the matrix
public void info(){
System.out.println("Sparse Matrix n="+size+", nnz="+nnz+", Storage="+(8*nnz)+"b or "+(8*nnz)/(1024*1024)+"Mb");
}
}