Hi everyone,

I'm trying to make a little applet that reads text files. I was planning to load some data into a class and use it later. However, I seem to having a few problems with it.

// DataSet.java
package com.someone.something;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DataSet {

    private String[] header;
    private List<String[]> data;
    private String[] tempArray;
    private int cCounter = 0;
    private int rCounter = 0;

    private String[][] finishedData;

    private boolean finished = false;

    public DataSet(int columns){
        data = new ArrayList<String[]>();
        tempArray = new String[columns];
        Arrays.fill(tempArray,"");
    }

    public void nextRow(){  
        data.add(rCounter,tempArray);   // Seems that this is the problem

        Arrays.fill(tempArray,"");
        cCounter = 0;
        rCounter++; 
    }

    public void add(String k){
        tempArray[cCounter] = k;
        cCounter++;
        finished = false;
    }

    public String[][] getData(){
        if(!finished){
            finish();
        }
        return finishedData;
    }

    public void finish(){
        finishedData = data.toArray(new String[data.size()][]);
        finished = true;
    }

}

This is the part that uses the DataSet class:

// Inside of the main class

DataSet dataSet;
String[][] data;
ArrayList<String[]> dataList;

dataList = new ArrayList<String[]>();

String[] a = {"aaa","aab","aac","aad","aae"};
String[] b = {"bbb","bba","bbc","bbd","bbe"};
String[] c = {"ccc","cca","ccb","ccd","cce"};

dataSet = new DataSet(5);

for(String value : a){
    dataSet.add(value);
}
dataSet.nextRow();
for(String value : b){
    dataSet.add(value);
}
dataSet.nextRow();
for(String value : c){
    dataSet.add(value);
}

dataSet.nextRow();

data = dataSet.getData();

String bufferedString = "";
data = dataSet.getData();
for(String[] rows : data){
    bufferedString = "";
    for(int i = 0; rows.length > i; i++){

        bufferedString += rows[i];
        if(rows.length - 1 != i){
            bufferedString += "\t";
        }

    }
    System.out.println(bufferedString);
}

When I run this example, I would get 5 tabs per line for 3 lines. When I tested the data that went into DataSet.Data in DataSet.nextRow(), every row seems to be the same instead of writing to the next index.

Can anyone help me? Thanks

You are re-using tempArray. When you add tempArray to data you are adding a reference to tempArray. Because you re-use the same array, every entry in data refers to the same array. Then you reset the contents of tempArray and overwrite with the next row's data.
You need to allocate a new array for ebery row.

commented: Yes that was the problem thanks +1

Ah yes that was the problem.
Thank you so much :)

I did not know that it took a reference and not a copy.

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.