java arraylist help needed
Have coded in many languages before but new to Java. I want to create an array whose members are of type
public class data
{
String date="";
String time="";
double price=0;
int volume=0;
};
data data1 = new data();
public ArrayList ftsedata = new ArrayList<data>();
I then get market data which i insert into data1 followed by
ftsedata.add(data1);
I then wait for fresh data and when i get it I insert it AGAIN into data1 and add it to ftsedata. Although ftsedata seems to be increasing in size correctly it seems that elements of ftsedata are just pointers to data1 and hence all elements of ftsedata seems to have values equal to the last data1 values.
I am using arraylist because I do not know how many datapoint will come in during the day so i need something that expands.
开发者_如何学CCould someone please tell me if I should be using arraylist or something else and if arraylist what is my problem.
many thanks for your time.
Many Thanks everyone,
I actually have four different functions that each fill one part of data1. Taking your views on board, i left data1 alone as a variable that can be seen in all functions. In the particular function that adds data1 to the arraylist i created a dataLocal variable of type Data and copied everything into it like dataLocal.price= data1.price; etc etc I then added dataLocal to the arraylist. This seems to work. I am assuming that each time this function is called a new local variable with the name dataLocal is created. Thanks you all for your help and please let me know if there is somethimng in this way of doing things that can get me in trouble later,eg, some other part of program would write over one instance of dataLocal because as far as the program is concerned the need for the variable is over.
Yes, an ArrayList<E>
(referred to by its interface List<E>
) looks to be the data structure that you need in this case.
The problem with the elements being references to the same object is best solved by making public class Data
an immutable value type. Then whenever you get new data, you will instantiate a new Data
object that you can put into the list.
See also
- Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces
- Effective Java 2nd Edition, Item 15: Minimize mutability
What you are putting into the ArrayList is actually the reference, not a copy of the object. So if you do:
data data1 = new data();
data1.price = 50;
ArrayList ftsedata = new ArrayList<data>();
ftsedata.add(data1);
data1.price = 500;
The price will change both in data1, and in the value contained in the ArrayList since both data 1 and frsedata.get(0) point to the same object. What you need to do is create a new data object for each new version you want to keep in the ArrayList, and make sure you don't modify the one that is already in there.
ArrayList ftsedata = new ArrayList<data>();
data data1 = new data();
//set values for properties of data1 here from wherever you are getting them
data1.price = 50;
...
ftsedata.add(data1);
data data2 = new data();
//set values for properties of data2 here from wherever you are getting them
data2.price = 500;
...
ftsedata.add(data2);
This way each entry in the ArrayList will be different.
You need to understand, for non-primitive variables, they are all "reference" (just treat it as C/C++ pointer)
ArrayList is storing list of "reference" to Data (please use Data instead of data for class name in Java).
Therefore what you need to do is, instead of reusing the Data instance that data1 is pointing at, you should instantiate a new instance of Data, put new incoming data in that new instance, and then add that new instance to ftsedata.
The ArrayList is the correct solution, but you just need to tweak how you use it. Adding a function to create a local object for each insertion will help in your situation. See below. I tweaked your code a little. I just whipped this up in Notepad, so I apologize for any errors. Hopefully the techniques shown will help.
public class Data
{
String date="";
String time="";
double price=0;
int volume=0;
};
public class DataModel
{
ArrayList<Data> ftsedata;
public DataModel()
{
ftsedata = new ArrayList<Data>();
}
public void addDataPoint()
{
Data dp = new Data();
dp.price = 50;
dp.volume = 10;
...
ftsedata.add(dp);
}
}
// In your main control part of the application or wherever
DataModel ftseDataModel = new DataModel();
while(something) // Maybe while you read a file? Check the server every so often
{
ftseDataModel.addDataPoint();
}
Try cloning your object before putting it into the ArrayList:
ftsedata.add(data1.clone());
精彩评论