Results 1 to 15 of 15
  1. #1
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Noob ArrayList Question

    So if I remove() an element from an ArrayList, does that automatically compact it so it has one fewer indices, or is the index still there with a null value? And if it is the latter, how do I compact it?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Noob ArrayList Question

    A simple way to find out: create an ArrayList, check its size, remove an element and recheck its size.

    Yeah, I know just giving the answer would be quicker, but it's more important that you learn the technique of using the Java compiler to help you learn Java than that we just give you the answer.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Noob ArrayList Question

    Read the trimToSize method in the API.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Noob ArrayList Question

    Quote Originally Posted by Junky View Post
    Read the trimToSize method in the API.
    Not to belittle this important method or your reply, but I don't think that this is necessary for him at this stage or is what he's looking for. He's not asking about the ArrayList's capacity -- his question is much more basic and instead revolves around the ArrayList's size, and the efficiency gains from this method are probably not all that important to him at this time.
    Last edited by Fubarable; 11-11-2011 at 12:42 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Noob ArrayList Question

    I thought OP was asking about capacity which is different from the size.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Noob ArrayList Question

    Quote Originally Posted by Koala View Post
    how do I compact it?
    Which is exactly what trimToSize should do.

  7. #7
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Noob ArrayList Question

    Okay, I did what Furable suggested:

    Java Code:
    import java.util.*;
    
    public class Template
    {
    
    	public static void main(String[] args)
    	{
    		ArrayList<Integer> someInts = new ArrayList<Integer>();
    		
    		for (int i = 0; i < 10; i++)
    		{
    			someInts.add(i);
    		}
    		
    		System.out.print(someInts.size() + " elements = ");
    		for (int i=0; i< someInts.size(); i++)
    		{
    			System.out.print(someInts.get(i) + " ");
    		}
    		System.out.print("\n\n");
    		
    		someInts.remove(3);
    		
    		System.out.print(someInts.size() + " elements = ");
    		for (int i=0; i< someInts.size(); i++)
    		{
    			System.out.print(someInts.get(i) + " ");
    		}
    		System.out.print("\n\n");
    
    	}
    }
    which prints:

    10 elements = 0 1 2 3 4 5 6 7 8 9

    9 elements = 0 1 2 4 5 6 7 8 9

    So I guess my first inclination was correct?

  8. #8
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Noob ArrayList Question

    Quote Originally Posted by Junky View Post
    Which is exactly what trimToSize should do.
    I will look that up too, thanks

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Noob ArrayList Question

    It depends upon what you are talking about. If you create a default ArrayList it will have a capacity of 10. Which means it can hold 10 items before it has to "grow" but its size will be 0 as there are no items in the list yet.

  10. #10
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Noob ArrayList Question

    Well here is where I'm running into trouble:

    Java Code:
    import java.util.*;
    
    public class CardGame
    {
    	public static void main(String[] args)
    	{
    		
    		// New deck of cards
    		ArrayList<Card> deck = new ArrayList<Card>();
    		
    		deck = initializeDeck(deck);
    		
    		deck = shuffleDeck(deck);
    		
    		displayCards(deck);
    	}
    	
    	private static ArrayList<Card> initializeDeck(ArrayList<Card> deck)
    	{
    		
    		for (int i=0; i<4; i++)
    		{
    			for (int j=0; j<13; j++)
    			{
    				deck.add(new Card(i, j));
    			}
    		}
    		
    		return deck;
    	}
    	
    	private static ArrayList<Card> shuffleDeck(ArrayList<Card> deck)
    	{
    		int index;
    		Random rd = new Random();
    		ArrayList<Card> newDeck = new ArrayList<Card>();
    		
    		for(int i=1; i>=52; i++)
    		{
    			index = rd.nextInt(52 - i);
    			newDeck.add(deck.get(index));
    			deck.remove(index);
    		}
    		
    		return newDeck;	
    	}
    	
    	private static void displayCards(ArrayList<Card> deck)
    	{
    		for(int i=0; i<52; i++)
    		{
    			System.out.println(deck.get(i).toString());
    		}
    	}
    }
    When I call shuffledeck(), my original ArrayList 'deck' has its size reduced to 0, and it returns newDeck which has a size of 52; then in the main method on line 13 I am storing the value of newDeck in the variable deck. When I run the program I get an index out of bounds exception on line 52 where it is trying to display the values of each card in deck. It still thinks the size of deck is 0, even though I replaced it with newDeck.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Noob ArrayList Question

    If you do not have to write the code yourself then use the Collections class to shuffle the cards.

    If you do have to write the code yourself then do the shuffle in place. By that I mean do not create a new List. Generate 2 random numbers and swap the cards at those locations. Repeat that X number of times.

    Once problem is you are assuming that the deck to be shuffled always contains 52 card. What if you want to shuffle a deack with only 30 cards?

  12. #12
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Noob ArrayList Question

    Quote Originally Posted by Junky View Post
    If you do not have to write the code yourself then use the Collections class to shuffle the cards.

    If you do have to write the code yourself then do the shuffle in place. By that I mean do not create a new List. Generate 2 random numbers and swap the cards at those locations. Repeat that X number of times.

    Once problem is you are assuming that the deck to be shuffled always contains 52 card. What if you want to shuffle a deack with only 30 cards?
    Well I would just replaced the hardcoded 52 with deck.size(). But yeah, I would like to write the method myself, if anything for the practice. I can try shuffling in place instead, thanks for the suggestion. I am still curious to figure out why my current code doesn't work, though...

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default Re: Noob ArrayList Question

    Quote Originally Posted by Koala View Post
    I am still curious to figure out why my current code doesn't work, though...
    Check out your for loop very carefully.
    Java Code:
    for(int i=1; i>=52; i++)

  14. #14
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Noob ArrayList Question

    Blah... okay i figured it out. Very trivial typo.

  15. #15
    Koala is offline Member
    Join Date
    Oct 2011
    Posts
    22
    Rep Power
    0

    Default Re: Noob ArrayList Question

    Quote Originally Posted by Junky View Post
    Check out your for loop very carefully.
    Java Code:
    for(int i=1; i>=52; i++)
    :facepalm:

Similar Threads

  1. noob question!
    By swp in forum New To Java
    Replies: 5
    Last Post: 09-28-2011, 11:14 PM
  2. noob question. plz help tho!
    By swp in forum New To Java
    Replies: 2
    Last Post: 09-28-2011, 06:16 AM
  3. Hi guys! Noob question...
    By moominboy in forum New To Java
    Replies: 1
    Last Post: 09-26-2011, 04:43 PM
  4. Probably Really Noob Question
    By bpx95 in forum New To Java
    Replies: 2
    Last Post: 05-16-2011, 01:44 AM
  5. Question - I'm a noob!
    By Insaeno in forum New To Java
    Replies: 5
    Last Post: 08-04-2008, 03:20 AM

Posting Permissions

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