我是java的新手,似乎无法解决问题。我正在尝试获取两个股票信息数组,并将它们相互比较(仅保留两个数组中出现的信息)。我读了一些关于泛型算法的内容,如果匹配,我希望能够创建类来为每个数组设置健康分数。我的代码并没有真正起作用(我可以用它来分析数组的每个组件,但不是我想要的范围)。为了清理,这里是我的数据样本:
ID date Ticker Shares
1 2011-06-19 goog 0
1 2011-06-19 ibm 0
1 2011-06-19 gs 0
1 2011-06-19 msft 0
1 2011-06-19 c 5
2 2011-06-19 goog 0
2 2011-06-19 ibm 0
2 2011-06-19 gs 0
2 2011-06-19 msft 1
2 2011-06-19 c 4
3 2011-06-19 goog 0
3 2011-06-19 ibm 0
3 2011-06-19 gs 0
3 2011-06-19 msft 2
3 2011-06-19 c 3
4 2011-06-19 goog 0
4 2011-06-19 ibm 0
4 2011-06-19 gs 0
4 2011-06-19 msft 3
4 2011-06-19 c 2
5 2011-06-19 goog 0
5 2011-06-19 ibm 0
5 2011-06-19 gs 0
5 2011-06-19 msft 4
5 2011-06-19 c 1
就这样,我有一个这样的数组和另一个前一个日期。我希望能够将两者相互比较(按id分组),并找到完整的匹配。但是后来,我希望能够通过其他课程获得成功的比赛并对其进行分析。我认为第一步是确定一场比赛。这是我的代码(它只识别股票代码/股票的匹配,我不知道如何让它匹配整个ID集):
public void compare(int firstindex, int lastIndex, Object date1, ArrayList data1id, ArrayList data1ticker, ArrayList data1shares, ArrayList data1price, Object date2, ArrayList data2id, ArrayList data2ticker, ArrayList data2shares, ArrayList data2price) throws Exception {
ArrayList ticker = new ArrayList();
ArrayList shares = new ArrayList();
ArrayList price = new ArrayList();
while (firstindex < lastIndex) {
//System.out.print("date is " + date1);
ticker.add(data1ticker.get(firstindex));
shares.add(data1shares.get(firstindex));
price.add(data1price.get(firstindex));
firstindex++;
}
comparewithsecondarray(ticker, shares, price, date2, data2id, data2ticker, data2shares, data2price);
//System.out.println("***********");
}
public void comparewithsecondarray(ArrayList tickerarray, ArrayList sharesarray, ArrayList pricearray, Object date2, ArrayList data2id, ArrayList data2ticker, ArrayList data2shares, ArrayList data2price) throws Exception {
//get the total number of values in the array
int totalArrayList = tickerarray.size();
int counter= 0;
System.out.println("Array has been checked against second array and we're on " + counter);
System.out.println(tickerarray);
System.out.println(sharesarray);
System.out.println("+++++++");
while (counter < totalArrayList) {
Object ticker = tickerarray.get(counter);
Object shares = sharesarray.get(counter);
Object price = pricearray.get(counter);
loadSecondArray(ticker, shares, price, date2, data2id, data2ticker, data2shares, data2price);
counter++;
}
}
public void loadSecondArray(Object ticker, Object shares, Object price, Object date2, ArrayList data2id, ArrayList data2ticker, ArrayList data2shares, ArrayList data2price) throws Exception {
//System.out.println("ticker " + ticker);
//System.out.println("shares " + shares);
//System.out.println("price " + price);
//find the last number of the arrray
if (!data2id.isEmpty()) {
int counter2 = Integer.parseInt(data2id.get(data2id.size()-1).toString());
//System.out.println("last element in array2 is " + counter2);
}
//location is the id number we're looking for.
int location = 1;
while (location > counter2) {
boolean blnFound = data2id.contains(location);
//System.out.println("Does arrayList contain " + location + "? " + blnFound);
if (blnFound) {
if(firstindex == -1) {
//System.out.println("ArrayList does not contain " + location);
} else {
//System.out.println("ArrayList contains " + location + " at index :" + firstindex);
int firstindex = data2id.indexOf(location);
int lastIndex = data2id.lastIndexOf(location);
//send ranges to study
while (firstindex < lastIndex) {
//System.out.print("date is " + date1);
Object ticker2 = data2ticker.get(firstindex);
Object shares2= data2shares.get(firstindex);
Object price2 = data2price.get(firstindex);
if (ticker.equals(ticker2) && shares.equals(shares2)) {
System.out.println("We have a match!");
System.out.println(ticker);
System.out.println(ticker2);
System.out.println(shares);
System.out.println(shares2);
System.out.println("*****");
}
//add to the counter
firstindex++;
}
location++;
}
} else {
break;
}
}
对于代码的质量提前抱歉,我很新,还在学习。我认为第一步是识别匹配,然后有办法将这些匹配(作为arraylists,我猜)传递给其他类进行分析。
关于如何实现我的项目目标的任何建议都会很棒(我正在读一本关于遗传算法的书,但它有点难以掌握,所以我开始回顾我在interne上找到的所有代码了解它是如何完成的。
提前致谢
答案 0 :(得分:1)
我想你可能需要这样的东西:
import java.util.Calendar;
//class representing all your data
public class StockData implements Comparable<StockData>{
private int id;
private Calendar date;
private List<ShareBean> shares;
//this will return whichever StockData that has more total shares as being greater
@Override
public int compareTo(StockData arg0) {
int totalshares = 0;
int totalshares2 = 0;
for(ShareBean share: shares)
totalshares+=share.getShares();
for(ShareBean share: arg0.getShares())
totalshares2+=share.getShares();
return totalshares-totalshares2;
}
//this method is used to see if another StockData object has the same id
@Override
public boolean equals(Object arg0) {
try {
StockData arg1 = (StockData) arg0;
if (id == arg1.id)
return true;
} catch (Exception e) {
return false;
}
return false;
}
public void setDate(Calendar date) {
this.date = date;
}
public Calendar getDate() {
return date;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setShares(List<ShareBean> shares) {
this.shares = shares;
}
public List<ShareBean> getShares() {
return shares;
}
public String toString(){
String toReturn = "";
toReturn+="ID: "+id+"\n";
toReturn+="Date: "+date.getTime()+"\n";
for(ShareBean share: shares)
toReturn+="Shares: "+share.toString()+"\n";
return toReturn;
}
}
使用此功能,您只需为每个数据对象创建一个StockData对象,并将其添加到此类对象的数组中。然后,如果您想知道它们是否相同,您只需使用StockData的.equals(Object arg0)
方法,并将其与另一个StockData对象进行比较。
例如:
//this method compares to Lists of StockData, and returns a list containing all
//the StockData objects that had matches
public List<StockData> comparewithsecondarray(List<StockData> StockData1, List<StockData> StockData2) {
List<StockData> list = new ArrayList<StockData>();
for(StockData sd1: StockData1){
for(StockData sd2: StockData2){
if(sd1.equals(sd2)){
//found a match! add it to the list
list.add(sd1);
//break so we don't add the same object multiple times
break;
}
}
}
return list;
}
看起来你真的让它变得非常复杂。如果您要重新发布您想要做的事情,那么这将更容易回答您的问题。
编辑:我修改了我的StockData类,并添加了另一个类来跟踪共享:
public class ShareBean { 私人字符串代码; 私人股份;
public ShareBean(String ticker, int shares){
this.ticker = ticker;
this.shares = shares;
}
public void setTicker(String ticker) {
this.ticker = ticker;
}
public String getTicker() {
return ticker;
}
public void setShares(int shares) {
this.shares = shares;
}
public int getShares() {
return shares;
}
public String toString(){
String toReturn = "";
toReturn+="Ticker: "+ticker+", Shares: "+shares;
return toReturn;
}
}
另一个编辑:
把这个主要方法放在某个地方......这并不重要。
public static void main(String[] args) {
List<StockData> listSD1 = new ArrayList<StockData>();
List<StockData> listSD2 = new ArrayList<StockData>();
StockData sd1 = new StockData();
StockData sd2 = new StockData();
List<ShareBean> listShares1 = new ArrayList<ShareBean>();
List<ShareBean> listShares2 = new ArrayList<ShareBean>();
//create the shares for sd1
listShares1.add(new ShareBean("goog", 3));
listShares1.add(new ShareBean("ibm", 5));
listShares1.add(new ShareBean("gs", 0));
listShares1.add(new ShareBean("msft", 0));
listShares1.add(new ShareBean("c", 1));
//create the shares for sd2
listShares2.add(new ShareBean("goog", 0));
listShares2.add(new ShareBean("ibm", 1));
listShares2.add(new ShareBean("gs", 3));
listShares2.add(new ShareBean("msft", 0));
listShares2.add(new ShareBean("c", 5));
//set their ids
sd1.setId(1);
sd2.setId(2);
//set the dates (using calendars)
sd1.setDate(Calendar.getInstance());
sd2.setDate(Calendar.getInstance());
//and finally set the shares
sd1.setShares(listShares1);
sd2.setShares(listShares2);
//now add each object to each list. the lists will be exacly the same
listSD1.add(sd1);
listSD1.add(sd2);
listSD2.add(sd1);
listSD2.add(sd2);
//now the lists are ready, and we can compare them
//I put the comparewithsecondarray method in the StockData class, but it could go anywhere
//I also overrode the "toString" method to make the output more readable (in both StockData and ShareBean)
System.out.println(Arrays.toString(sd1.comparewithsecondarray(listSD1, listSD2).toArray()));
}