Results 1 to 15 of 15
Thread: Noob ArrayList Question
- 11-11-2011, 12:20 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
-
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.
- 11-11-2011, 12:39 AM #3
Re: Noob ArrayList Question
Read the trimToSize method in the API.
-
Re: Noob ArrayList Question
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.
- 11-11-2011, 12:48 AM #5
Re: Noob ArrayList Question
I thought OP was asking about capacity which is different from the size.
- 11-11-2011, 12:49 AM #6
- 11-11-2011, 12:55 AM #7
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Noob ArrayList Question
Okay, I did what Furable suggested:
which prints: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"); } }
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?
- 11-11-2011, 12:56 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
- 11-11-2011, 01:10 AM #9
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.
- 11-11-2011, 01:29 AM #10
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Noob ArrayList Question
Well here is where I'm running into trouble:
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.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()); } } }
- 11-11-2011, 01:40 AM #11
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?
- 11-11-2011, 01:44 AM #12
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Noob ArrayList Question
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...
- 11-11-2011, 01:55 AM #13
- 11-11-2011, 01:56 AM #14
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Re: Noob ArrayList Question
Blah... okay i figured it out. Very trivial typo.
- 11-11-2011, 01:56 AM #15
Member
- Join Date
- Oct 2011
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
noob question!
By swp in forum New To JavaReplies: 5Last Post: 09-28-2011, 11:14 PM -
noob question. plz help tho!
By swp in forum New To JavaReplies: 2Last Post: 09-28-2011, 06:16 AM -
Hi guys! Noob question...
By moominboy in forum New To JavaReplies: 1Last Post: 09-26-2011, 04:43 PM -
Probably Really Noob Question
By bpx95 in forum New To JavaReplies: 2Last Post: 05-16-2011, 01:44 AM -
Question - I'm a noob!
By Insaeno in forum New To JavaReplies: 5Last Post: 08-04-2008, 03:20 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks