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?

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.

This article has been dead for over six months. Start a new discussion instead.