I got a program that loads in raw data for charting and stores it in a class called cRawGraph.. It then formats this data and stores it in another class called cFormatGraph. Is there a way to copy some of the date objects stored in cRwGraph to date objects stored in cFormattedGraph without using a reference? I looked at Oracle's documentation and did not see a constructor that would take in a date object or any methods data would accomplish this.
code snippet:
do{
d=rawData.mDate[i].getDay();
da=rawData.mDate[i];
datept=i;
do{
vol+=rawData.mVol[i];
pt+=rawData.mPt[i];
c++;
i++;
if (i>=rawData.getSize())
break;
} while(d==rawData.mDate[i].getDay());
// this IS NOT WORKING BECOUSE IT IS A REFRENCE AND RawData gets loaded with new dates,
// Thus chnaging the value in mDate
mDate[ii]=da;
mVol[ii]=vol;
mPt[ii]=pt/c;
if (first)
{
smallest=biggest=pt/c;
first=false;
}
else
{
double temp=pt/c;
if (temp
smallest=temp;
if (temp>biggest)
biggest=temp;
}
ii++;
} while(i
解决方案
You could use getTime() and passing it into the Date(time) constructor. This is only required because Date is mutable.
Date original = new Date();
Date copy = new Date(original.getTime());
If you're using Java 8 try using the new java.time API which uses immutable objects. So no need to copy/clone.