Results 1 to 8 of 8
  1. #1
    g2beastie is offline Member
    Join Date
    Mar 2010
    Posts
    2
    Rep Power
    0

    Exclamation Problem with ArrayLists

    Hello. I am working on an assigment with ArrayLists. The assignment asks us to create three arrayLists. It gives us 10 values for 2 of them and we're supposed to multiply the values of those 2 lists (in a parallel way) and store the results on the 3rd list. I've tried using a for loop and simply trying to multiply the lists using and index but it obviously doesn't work. I've tried converting the lists to arrays with the toArray() method, but it won't let me multiply the values either. I've tried storing the values in an array first and THEN store the array in the list, but I couldn't figure that one out. Any ideas on what I could try here. I'd appreciate it, Thank you. Here's what I have:

    Java Code:
    import java.util.ArrayList;
    
    
    public class GuessGame {
    	
    	public void main(String[] args){
    		
    		ArrayList <Double>priceList = new ArrayList(10);
    		ArrayList <Double>quantityList = new ArrayList(10);
    		ArrayList <Double>amountList = new ArrayList(10);
    		
    		//Populate priceList
    		priceList.add(10.62);priceList.add(14.89);priceList.add(13.21);
    		priceList.add(16.55);priceList.add(18.62);priceList.add(9.47);
    		priceList.add(6.58);priceList.add(18.32);priceList.add(12.15); 
    		priceList.add(3.98);
    		
    		//Populate quantityList
    		quantityList.add(4.0);quantityList.add(8.5);quantityList.add(6.0);
    		quantityList.add(7.35);quantityList.add(9.0);quantityList.add(15.3);
    		quantityList.add(3.0);quantityList.add(5.4);quantityList.add(2.9);
    		quantityList.add(4.8);
    	}
    	
    	public ArrayList extend(ArrayList a, ArrayList b, ArrayList c){
    		
    		//Multiplication of values should be done in here.
    		
    		return c;
    	}
    
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Your main method is incorrect. It should be,

    Java Code:
    public static void main(String[] args)

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Go through the following code and try to understand it.

    Java Code:
        public ArrayList extend(ArrayList a, ArrayList b, ArrayList c) {
    
            for (int i=0; i<a.size(); i++) {
                double val1 = Double.parseDouble(a.get(i).toString());
                double val2 = Double.parseDouble(b.get(i).toString());
    
                c.add((val1 * val2));
            }
            return c;
        }

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    His ArrayLists are already defined to hold Doubles only, so this:
    Java Code:
    ArrayList<Double> a, b, result;
    //populating a and b, same size for both
    for(int i = 0; i < a.size(); i++)
      result.add(a.get(i)*b.get(i));
    would still be valid, no casting is required. Even if the lists would return Objects, wouldn't simply casting them into Doubles be simpler than converting them to Strings and then parsing them to Double?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default

    Java Code:
    ArrayList <Double>priceList = new ArrayList(10);
    ArrayList <Double>quantityList = new ArrayList(10);
    ArrayList <Double>amountList = new ArrayList(10);
    Didn't your compiler whine about these lines? I would've done:

    Java Code:
    ArrayList <Double>priceList = new ArrayList<Double>(10);
    ArrayList <Double>quantityList = new ArrayList<Double>(10);
    ArrayList <Double>amountList = new ArrayList<Double>(10);
    kind regards,

    Jos

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by m00nchile View Post
    His ArrayLists are already defined to hold Doubles only, so this:
    Java Code:
    ArrayList<Double> a, b, result;
    //populating a and b, same size for both
    for(int i = 0; i < a.size(); i++)
      result.add(a.get(i)*b.get(i));
    would still be valid, no casting is required. Even if the lists would return Objects, wouldn't simply casting them into Doubles be simpler than converting them to Strings and then parsing them to Double?
    Yeah that's true. But I don't want to confuse OP. That's why I guide him in very simple level. Without his comments on that I don't want to give an effective answer in details. Otherwise they are not think about it. I like to know them what I know in that way. :)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by JosAH View Post
    Java Code:
    ArrayList <Double>priceList = new ArrayList(10);
    ArrayList <Double>quantityList = new ArrayList(10);
    ArrayList <Double>amountList = new ArrayList(10);
    Didn't your compiler whine about these lines? I would've done:

    Java Code:
    ArrayList <Double>priceList = new ArrayList<Double>(10);
    ArrayList <Double>quantityList = new ArrayList<Double>(10);
    ArrayList <Double>amountList = new ArrayList<Double>(10);
    kind regards,

    Jos
    May be his IDE not complain those warnings to the user.

Similar Threads

  1. [SOLVED] A better way to do this - ArrayLists
    By Tshegofatsom in forum New To Java
    Replies: 13
    Last Post: 05-20-2009, 02:05 PM
  2. how to compare the elements of these two arraylists
    By raj reddy in forum Web Frameworks
    Replies: 1
    Last Post: 03-25-2009, 10:55 PM
  3. ArrayList of ArrayLists
    By coolnfunky_raj in forum New To Java
    Replies: 10
    Last Post: 07-03-2008, 10:07 AM
  4. A Map implemented with ArrayLists
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:29 PM
  5. arraylists problem
    By newtojava7 in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 07:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •