【问题标题】:my array elements have the same id in Java?我的数组元素在 Java 中具有相同的 id?
【发布时间】:2014-10-24 21:30:10
【问题描述】:

这基本上是一个将两个稀疏矩阵相乘的程序。我有一个 SpEntry 类型的数组(一个具有 3 个即时变量、int row、column、value & getter & setter 方法的类),当我想设置为我的对象创建的数组元素时(m_1 和 m_2 是 SpArray 类的实例& 对于每一个都是一个 SpEntry[] spArray,其非零元素的大小) 当我想设置每个数组的元素时,我喜欢 m_1[0] 和 m_1[1](m_1 数组的元素)具有相同的 id,因此当我设置 m_1[0] 的行时, col,val 元素在 m_1[1] 的 row,col,val 中也是重复的

public class SpArray {
    public static int N ; //size of NxN matrix
    private SpEntry[] spArray;

    public SpArray(int nNZ){
        spArray = new SpEntry[nNZ];  //nNZ is the number of Non-Zero elements of the sparse matrix

        SpEntry init = new SpEntry(0,0,0);
        for (int s=0; s<spArray.length ; s++){
            spArray[s]= init ; 

        }

    }




    //returns the spArray
    public SpEntry[] getSpArray (){
        return this.spArray;
    }




    //returns the spArray elements
        public SpEntry getSpArray (int index){
            return this.spArray[index];
        }

这是 SpArray 类的一部分 & 在主程序中:

 public static void main(String[] args) {
    int nnz_1;
    int nnz_2;
    SpArray m_1;
    SpArray m_2;


    input = new Scanner (System.in);

    System.out.println("Please enter the size of your square sparse matrix: ");
    SpArray.N = input.nextInt();

    //getting the non zero elements of the 1st sparse matrix & making its sparse array

System.out.println("请输入第一个稀疏矩阵中非零元素的个数:"); nnz_1 = input.nextInt();

    m_1 = new SpArray (nnz_1);

    for(int j_1=0; j_1<nnz_1 ; j_1++){
    System.out.println("Enter number "+ (j_1+1) + " non-zero element of the 1st sparse matrix");
        System.out.println("row: ");
        int r_1 = input.nextInt();
        System.out.println("column: ");
        int c_1 = input.nextInt();
        System.out.println("Value: ");
        int v_1 = input.nextInt();


        /*
        SpEntry e_1 = new SpEntry (r_1, c_1, v_1);
        m_1.getSpArray()[j_1].setRow(e_1.getRow());
        m_1.getSpArray()[j_1].setCol(e_1.getCol());
        m_1.getSpArray()[j_1].setVal(e_1.getVal());
        */

        //new versiin revised
        SpEntry e_1 = new SpEntry (r_1, c_1, v_1);
        m_1.getSpArray(j_1).setRow(e_1.getRow());
        m_1.getSpArray(j_1).setCol(e_1.getCol());
        m_1.getSpArray(j_1).setVal(e_1.getVal());**

    } 

当我调试程序时,它显示 m_1 数组的元素具有相同的 id(和 m_2 的相同故事):

m_1           Value: SpArray (id=21)`enter code here`
  spArray            SpEntry[2] (id=22)
    [0]              SpEntry   (id=661)
    [1]              SpEntry   (id=661)

【问题讨论】:

    标签: java jquery arrays


    【解决方案1】:

    你的问题在这里:

    public SpArray(int nNZ){
        spArray = new SpEntry[nNZ];  //nNZ is the number of Non-Zero elements of the sparse matrix
    
        SpEntry init = new SpEntry(0,0,0);       // Problem here
        for (int s=0; s<spArray.length ; s++){
            spArray[s]= init ; 
    
        }
    
    }
    

    您只创建一个对象,然后用同一个对象填充整个矩阵。

    如果您希望对象不同,则需要为每个元素创建一个新对象:

    public SpArray(int nNZ){
        spArray = new SpEntry[nNZ];  //nNZ is the number of Non-Zero elements of the sparse matrix
    
        for (int s=0; s<spArray.length ; s++){
            spArray[s]= new SpEntry(0,0,0) ; 
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 1970-01-01
      • 2017-06-13
      • 2017-06-09
      • 1970-01-01
      • 2017-05-29
      • 2011-02-27
      相关资源
      最近更新 更多