here is my code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
/**
 *
 * @author Sugirthan
 */
class Node<E> {
    int ser;
    public double[] data;
     public Node(int ser,double[]data) {
        this.ser=ser;
        this.data = data;

    }
}
public class Tree<E> {
    Node a;
        double [][] data = new double [3][12];
         private void Read_File() throws FileNotFoundException, IOException{
             File file = new File("capital.txt");
            int row = 0;
            int col = 0;
            BufferedReader bufRdr  = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = bufRdr.readLine()) != null && row < data.length)
            {
                StringTokenizer st = new StringTokenizer(line,"\t");
                while (st.hasMoreTokens()){
                    try {
                        data[row][col] = Double.parseDouble(st.nextToken());
                    } catch (NumberFormatException e) {
                    }
                    col++;
                }
                col = 0;
                row++;
            }
            Set_Data(data);
         }
         private void Set_Data(double [][]inputdata){
             ArrayList<Node> tem=new ArrayList<Node>();
             double[] temin=new double[inputdata[0].length];
             double[] d1=new double[inputdata[0].length];
             int n=0;
             for(int i=0;i<inputdata.length;i++){
                 for(int j=0;j<inputdata[0].length;j++){
                     temin[j]=inputdata[i][j];
                 }
                 a=(Node) new Node(i+1,temin);
                 tem.add(a);
                 d1=tem.get(i).data;
                 for(int b=0;b<d1.length;b++){
                    System.out.print(d1[b]+" ");
                 }
                 System.out.println();
                //tem.get(n).setData(temin);
                 n++;
             }
             for(int i=0;i<tem.size();i++){
                 d1=tem.get(i).data;
                 System.out.println(tem.get(i).ser);
                 for(int b=0;b<d1.length;b++){
                    System.out.print(d1[b]+" ");
                 }
                 System.out.println();
             }
         }
    public static void main(String[] args) throws FileNotFoundException, IOException{
        Tree<Double> t=new Tree<Double>();
        t.Read_File();
    }
    }

output is
-3.0 -5.0 -1.0 3.0 10.0 13.0 16.0 15.0 10.0 6.0 1.0 -2.0
-3.0 0.0 6.0 13.0 20.0 24.0 26.0 25.0 20.0 13.0 5.0 -1.0
0.0 -1.0 4.0 7.0 12.0 16.0 18.0 17.0 14.0 9.0 4.0 1.0
1
0.0 -1.0 4.0 7.0 12.0 16.0 18.0 17.0 14.0 9.0 4.0 1.0
2
0.0 -1.0 4.0 7.0 12.0 16.0 18.0 17.0 14.0 9.0 4.0 1.0
3
0.0 -1.0 4.0 7.0 12.0 16.0 18.0 17.0 14.0 9.0 4.0 1.0

here serial no of Node is correct but ayyay data of all Node has been changed by last value. what is the reason?

Dani AI

Generated

The symptom (every Node showing the last row) is classic aliasing: a single double[] is filled repeatedly and each Node stores a reference to that same array. After the loop finishes the array contains the final row, so every Node appears to have the same data. As noted, the simplest fix is to stop reusing one array for every row — create a fresh array each iteration or make the Node take a defensive copy.

Two robust options:

  • Allocate a new row array inside the loop so each Node gets its own array.
  • Make Node copy the array it receives (defensive copy) and expose only copies from any getter, preventing external modification.

Example of a safe Node that makes a defensive copy:

import java.util.Arrays;

class Node {
    final int ser;
    private final double[] data;

    Node(int ser, double[] src) {
        this.ser = ser;
        this.data = Arrays.copyOf(src, src.length); // copy once here
    }

    double[] getData() {
        return Arrays.copyOf(data, data.length); // return a copy to callers
    }
}

If you prefer to keep Node unchanged, clone the row when adding it:

double[] row = /* build row values */;
tem.add(new Node(i + 1, row.clone())); // pass a copy so nodes don't share the same array

Quick checks/troubleshooting:

  • Print tem.get(0).getData() == tem.get(1).getData() (or use System.identityHashCode) to confirm whether arrays are the same object.
  • Make data private and immutable where possible to avoid accidental later modification.
  • Also tighten generics and local scope (declare the Node variable inside the loop, avoid raw types) to improve clarity and safety.

When you create the first Node the data variable is set to a reference to the array d1. You then change the values in d1 and create second node. But the first node is still pointing to d1, so when you change the values in d1 you affect the first node as well as the second.
The fix is to create a new d1 array for each node, ie move line 47 to be just inside the loop that starts at line 49.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.