简体   繁体   English

java arraylist需要帮助

[英]java arraylist help needed

Have coded in many languages before but new to Java. Java以前有过许多语言的编码,但对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 然后我得到市场数据,然后将其插入到data1中

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. 然后,我等待新数据,当我得到它时,我再次将其插入data1并将其添加到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. 尽管ftsedata的大小似乎正确增加,但ftsedata的元素似乎只是指向data1的指针,因此ftsedata的所有元素似乎都具有等于最后一个data1值的值。

I am using arraylist because I do not know how many datapoint will come in during the day so i need something that expands. 我使用arraylist是因为我不知道白天会输入多少数据点,因此我需要一些扩展的东西。

Could someone please tell me if I should be using arraylist or something else and if arraylist what is my problem. 有人可以告诉我我是否应该使用arraylist或其他东西,如果arraylist是我的问题,那可以。

many thanks for your time. 非常感谢您的宝贵时间。


Many Thanks everyone, 非常感谢大家,

I actually have four different functions that each fill one part of data1. 我实际上有四个不同的函数,每个函数都填充data1的一部分。 Taking your views on board, i left data1 alone as a variable that can be seen in all functions. 考虑到您的看法,我将data1单独作为一个变量,可以在所有函数中看到。 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; 在将data1添加到arraylist的特定函数中,我创建了Data类型的dataLocal变量并将所有内容复制到其中,例如dataLocal.price = data1.price;。 etc etc I then added dataLocal to the arraylist. 等等等然后我将dataLocal添加到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. 我假设每次调用此函数时都会创建一个名为dataLocal的新局部变量。 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. 谢谢大家的帮助,请让我知道是否有这种处理方式可能会在以后给我带来麻烦,例如,程序的其他部分将覆盖dataLocal的一个实例,因为就程序而言有关变量的需求已经结束。

Yes, an ArrayList<E> (referred to by its interface List<E> ) looks to be the data structure that you need in this case. 是的,在这种情况下, ArrayList<E> (由其接口List<E>引用)看起来是您需要的数据结构。

The problem with the elements being references to the same object is best solved by making public class Data an immutable value type. 通过将public class Data设为不可变值类型,可以最好地解决将元素引用到同一对象的问题。 Then whenever you get new data, you will instantiate a new Data object that you can put into the list. 然后,每当获取新数据时,都将实例化一个新的Data对象,该对象可以放入列表中。

See also 也可以看看

  • Effective Java 2nd Edition, Item 52: Refer to objects by their interfaces 有效的Java 2nd Edition,项目52:通过其接口引用对象
  • Effective Java 2nd Edition, Item 15: Minimize mutability 有效的Java 2nd Edition,第15项:最小化可变性

What you are putting into the ArrayList is actually the reference, not a copy of the object. 您放入ArrayList中的实际上是引用,而不是对象的副本。 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. 价格将在data1和ArrayList中包含的值中改变,因为data 1和frsedata.get(0)都指向同一对象。 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中的每个新版本创建一个新的数据对象,并确保您不修改已存在的对象。

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. 这样,ArrayList中的每个条目都会不同。

You need to understand, for non-primitive variables, they are all "reference" (just treat it as C/C++ pointer) 您需要了解,对于非原始变量,它们都是“引用”(只需将其视为C / C ++指针)

ArrayList is storing list of "reference" to Data (please use Data instead of data for class name in Java). ArrayList正在存储对数据的“引用”列表(请在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. 因此,您需要做的是,而不是重新使用data1指向的Data实例,您应该实例化Data的新实例,将新的传入数据放入该新实例,然后将该新实例添加到ftsedata中。

The ArrayList is the correct solution, but you just need to tweak how you use it. ArrayList是正确的解决方案,但是您只需要调整使用方式即可。 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();
}

在将对象放入ArrayList之前,请尝试对其进行克隆:

ftsedata.add(data1.clone());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM