I’m new to java and can’t seem to wrap my head around a problem. I’m trying to take two arrays of stock information, and compare them against each other(only keeping the ones that appear in both arrays). I read a bit about generic algorithms and if a match, I wanted to be able to create classes to assign fitness scores to each array set. My code doesn’t really work(I can get it to analyze each individual components of the array but not the range I want it to). To clear things up, here’s a sample of my data:
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
As so on, I have a array of this and another one for the previous date. I want to be able to compare(grouped by the id’s) the two against each other, and find entire matches. But later on, I want to be able to take the successful matches and perform analytic on them via other classes. I think the first step is identifying a match. Here’s my code(it only identifies a match of ticker/shares, and I’m not sure how to get it to match an entire ID set):
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;
}
}
Sorry in advance for the quality of the code, I’m pretty new and still learning. I think the first step is to identify the matches, then have a way to pass those matches(as arraylists, I guess) to other classes to analyze.
Any suggestions on how to achieve my goals of this project would be great(I am reading a book on genetic algo’s but its a bit hard to grasp so I’m starting to review all the code I can find on the interne to understand how its being done).
Thanks in advance
I think you may need something like this:
Using this, you just make a StockData object for every datapiece you have, and add it to an array of such objects. Then, should you wish to find out if they are the same, you just use the
.equals(Object arg0)method of StockData, and compare it to another StockData object.For instance:
It really looks like you have made this incredibly more complex than it needs to be. If you were to repost what SPECIFICALLY you want to do, it would make answering your question easier.
EDIT: I’ve modified my StockData class, and added this other class to keep track of shares:
public class ShareBean {
private String ticker;
private int shares;
ANOTHER EDIT:
Put this main method somewhere… it doesn’t really matter.