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];

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

        cCounter = 0;

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

    public String[][] getData(){
        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){
for(String value : b){
for(String value : c){


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";


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.