Results 1 to 5 of 5
  1. #1
    JaredXIII is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Question Working with ArrayLists, program locks up at runtime

    I've got this code fully built where I'm supposed to have three ArrayLists, save arrays of ten double variables to two of them, and then multiply the first two ArrayLists together and put the results in the third. I have it set up in a GUI, but the GUI is in an entirely separate class, and all it does is call an instance of the print() function, so the problem isn't there. Can anybody figure out why this program doesn't have any error messages yet locks up and eventually triggers and out of memory exception?

    Java Code:
    import java.util.*;
    
    public class ThreeArrayList{
    	
    	private double[] PRICE_ARRAY = {10.62, 14.89, 13.21, 16.55, 18.62, 9.47, 6.58, 18.32, 12.15, 3.98}; 
    	private double[] QUANTITY_ARRAY = {4.0, 8.5, 6.0, 7.35, 9.0, 15.3, 3.0, 5.4, 2.9, 4.8};
    	private ArrayList<Double> priceList;
    	private ArrayList<Double> quantityList;
    	private ArrayList<Double> amountList;
    	private String output = "";
    	
    	public ThreeArrayList(){
    		
    		priceList = new ArrayList<Double>(15);
    		quantityList = new ArrayList<Double>(15);
    		amountList = new ArrayList<Double>(15);
    		
    	}
    	
    	public void setPL(){
    		for(int i = 0; i < PRICE_ARRAY.length; i++)
    			priceList.add(PRICE_ARRAY[i]);
    	}
    	
    	public ArrayList<Double> getPL(){
    		setPL();
    		priceList.trimToSize();
    		return priceList;
    	}
    	
    	public void setQL(){
    		for(int i = 0; i < QUANTITY_ARRAY.length; i++)
    			quantityList.add(QUANTITY_ARRAY[i]);
    	}
    	
    	public ArrayList<Double> getQL(){
    		setQL();
    		quantityList.trimToSize();
    		return quantityList;
    	}
    	
    	public void extend(){
    		for (int i = 0; i < getPL().size(); i++){
    			//amountList.add(priceList.get(i) * quantityList.get(i));
    			amountList.add(getPL().get(i)*getQL().get(i));
    		}
    		//return amountList;
    	}
    	
    	public ArrayList<Double> getAL(){
    		extend();
    		amountList.trimToSize();
    		return amountList;
    	}
    	
    	public String print(){
    		for (int i = 0; i < getPL().size(); i++){
    			output = output.concat(i+") "+getPL().get(i)+" * "+getQL().get(i)+ " = "+getAL().get(i)+"\n");
    		}
    		return output;
    	}
    	
    	public static void main(String[] args) {
    		ThreeArrayListtal = new ThreeArrayList();
    		System.out.print(tal.print());
    	}
    
    }
    Thank you very much for any help you can offer!
    Last edited by pbrockway2; 04-06-2012 at 12:21 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Working with ArrayLists, program locks up at runtime

    Hi JaredXIII, welcome to the forums!

    When you post code, use the "code" tags: put [code] at the start of the code and [/code] at the end. That way the code will be readable when it appears here.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Working with ArrayLists, program locks up at runtime

    There's a bit of confusion in the code. Look at the get/setPL() methods. getPL() calls setPL() and setPL() adds a whole lot of entries to the priceList array. The important point to note is that a number of new entries will be added to the priceList array every time getPL() is called.

    Now look at the loop in the print() method. The stopping condition is "i<getPL().size();" but, as we have seen getPL() causes the returned list to grow in size. Every time the stopping condition is checked it recedes further away from ever being true. (to make matters worse there is another call to getPL() within the body of the loop).

    Despite its innocent sounding name getPL() is doing more than merely fetching something. It is causing the thing-being-got to grow in size. This sort of thing is called a "side effect" and your code will stand the best chance of being error free if you can reduce or eliminate such side effects.

    -----

    Consider a getPL() that does just that: returns the list. Ie, doesn't call setPL() as a side effect.

    This leaves you with the question of how the list will ever get populated. There are a couple of approaches I can think of, but the easiest is to populate the array in the constructor. The constructor's job is to put the instance into a usable state and you could consider the list to be usable only when it has stuff put into it. Since the constructor is called just once per instance there will be no danger of the list subsequently growing.

    (If you really want to have a setPL() method - and it may be a requirement of some assigned homework or whatever - then ensure that it only puts the values into the list once. It could check the size of the list before it adds the values from the arrays.)

    These remarks apply to all the set/get methods. Let the setter methods set; safely without stuffing more and more into the list. Let the getter methods get; and not call setter methods or do anything else as a side effect.

    -----

    Why do you call trimToSize() on the lists? I mean "why" in terms of the behaviour of this method as stated in its API documentation, rather than what you thought/guessed/hoped would be its effect. That documentation also discusses what is meant by "capacity". If you don't have a reason, consider removing those lines as being more over complication.

    Why is print() called print? It doesn't print anything. Overriding toString() might be better, or some other name for the method that describes what it does. I think this is more than just being fussy. The whole burden of this post is to suggest "one job: one method". And part of that is having a clear idea of the intended behaviour (job) of each method: something that can be usefully expressed as part of the method's name (and its javadoc).

  4. #4
    JaredXIII is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Working with ArrayLists, program locks up at runtime

    This was extremely helpful, and my code works now. I took your advice and eliminated the trimToSize statements, as I had mainly put them in there when playing around with how to make this work. I put the setPL() and setQL() methods in the constructor, and only called the setAL() method from within the print() method. Overriding toString() is a good idea, and I do understand it is basically just for aesthetics, since both work. Thank you so much for your help!

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Working with ArrayLists, program locks up at runtime

    You're welcome.

Similar Threads

  1. Synchronization utility to manage locks
    By JSL5101 in forum Advanced Java
    Replies: 1
    Last Post: 05-07-2011, 05:43 PM
  2. Converting Program from ArrayLists to Arrays
    By Java-Guy in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 09:02 AM
  3. InterruptedException and Locks
    By JavaJuJitZu in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-19-2010, 02:47 AM
  4. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  5. I can't fix this runtime error in my program
    By Sinnergy in forum New To Java
    Replies: 5
    Last Post: 02-25-2009, 04:29 PM

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
  •