Results 1 to 15 of 15

Thread: Array question.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2003
    Posts
    108

    Array question.

    Hi,

    I have 2 questions:

    #1) How do you check if the array is empty?

    #2) How do you continue to get the item of the last element of the array? Assumingly the array is not empty.
    Example: myArray[] FACES = {"10","J","Q","K","A"}; I'd like to get the values in the array and print them out in the reverse order. A,K,Q,J,10.

    Thanks

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Array question.

    Code:
    #1) if(array==null)
    
    #2) for (int i = FACES.length - 1 ;i => 0 ; i++){
               FACES[i];
          }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Array question.

    Is it possible to have an array that's empty but not null ie array.length==0

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Array question.

    you mean like
    Code:
    int[] array = new int [0] ;
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Lively Member
    Join Date
    May 2002
    Posts
    118

    Re: Array question.

    Thank you!

  6. #6
    Lively Member
    Join Date
    May 2002
    Posts
    118

    Re: Array question.

    for the question #2, I did exactly what you said and I got an error message "Illegal start of expression".

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Array question.

    Quote Originally Posted by AlnavPlatinum
    for the question #2, I did exactly what you said and I got an error message "Illegal start of expression".
    Show me your code
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  8. #8
    Lively Member
    Join Date
    May 2002
    Posts
    118

    Re: Array question.

    public Card drawCard()
    {

    if(cards!= null)
    {
    for(int intCnt = cards.length - 1; intCnt => 0; inCnt++)
    {
    cards[inCnt];
    }
    }

    }

    The execution line stops at:for(int intCnt = cards.length - 1; intCnt => 0; inCnt++)

  9. #9
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Array question.

    Code:
    for(int intCnt = cards.length - 1; intCnt >= 0; inCnt++)
    how stupid I was, So so
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  10. #10
    Lively Member
    Join Date
    May 2002
    Posts
    118

    Re: Array question.

    Got it.. Thanks

  11. #11
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: Array question.

    Code:
    boolean notFull = false;
    for(int i = 0; i < arrayName.length; i+)
    {
    	try{
    		Object o = arrayName[i];
    	}catch(NullPointerException ex)
    	{
    		notFull = true;
    	}
    }
    That will tell you if there are any empty elements.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Array question.

    No, it won't. Just accessing and storing the reference does not throw a null pointer exception. The only way your code can catch one is if the array itself is null.
    Also, exceptions should never be used for normal computation tasks.

    Just compare each element to null.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Lively Member
    Join Date
    Oct 2005
    Posts
    74

    Re: Array question.

    I take that back then, I had not tested it, I thought it would work.
    If the variable in the array is null why wouldn't it throw a NullPointerException?

  14. #14
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Array question.

    If you tried using the "o" var it would have. but since you didn't do anything to upset the JVM it'll stay putt
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  15. #15
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Array question.

    Code:
    for(int intCnt = cards.length - 1; intCnt >= 0; inCnt++)
    should be:
    Code:
    for(int intCnt = cards.length - 1; intCnt >= 0; intCnt--)
    {
       System.out.print (cards[intCnt]);
    }
    Otherwise it would be an infinite loop, intCnt would always be greater than 0. (that is assuming you just made a typo on your last variable name and it's supposed to be intCnt not inCnt, if it's not a typo, then ignore this.)
    Last edited by lunchboxtheman; Oct 10th, 2006 at 04:21 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width