We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,489 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

ArrayList String arrays problem

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

2
Contributors
2
Replies
1 Hour
Discussion Span
9 Months Ago
Last Updated
3
Views
Question
Answered
iThaos
Light Poster
42 posts since May 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,533 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

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

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

iThaos
Light Poster
42 posts since May 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 9 Months Ago by JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.1030 seconds using 2.7MB