Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6067153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:34:39+00:00 2026-05-23T09:34:39+00:00

I’m new to java and can’t seem to wrap my head around a problem.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:34:40+00:00Added an answer on May 23, 2026 at 9:34 am

    I think you may need something like this:

    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;
    }
    }
    

    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:

    //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;
    }
    

    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;

    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;
    }
    }
    

    ANOTHER EDIT:

    Put this main method somewhere… it doesn’t really matter.

    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()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.