Click to See Complete Forum and Search --> : Array question.
Academy
Sep 26th, 2006, 02:06 AM
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
ComputerJy
Sep 26th, 2006, 08:11 AM
#1) if(array==null)
#2) for (int i = FACES.length - 1 ;i => 0 ; i++){
FACES[i];
}
DeadEyes
Sep 26th, 2006, 10:11 AM
Is it possible to have an array that's empty but not null ie array.length==0
ComputerJy
Sep 26th, 2006, 10:23 AM
you mean like int[] array = new int [0] ;
AlnavPlatinum
Sep 26th, 2006, 10:24 AM
Thank you!
AlnavPlatinum
Sep 26th, 2006, 10:49 AM
for the question #2, I did exactly what you said and I got an error message "Illegal start of expression".
ComputerJy
Sep 26th, 2006, 11:11 AM
for the question #2, I did exactly what you said and I got an error message "Illegal start of expression".
Show me your code
AlnavPlatinum
Sep 26th, 2006, 12:05 PM
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++)
ComputerJy
Sep 26th, 2006, 12:19 PM
http://img400.imageshack.us/img400/8008/oops7yt.giffor(int intCnt = cards.length - 1; intCnt >= 0; inCnt++)
how stupid I was, So so http://imagehost.biz/ims/pictes/209208.gif
AlnavPlatinum
Sep 26th, 2006, 12:28 PM
Got it.. Thanks
vagabon
Oct 9th, 2006, 08:13 AM
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.
CornedBee
Oct 9th, 2006, 10:38 AM
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.
vagabon
Oct 10th, 2006, 09:26 AM
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?
ComputerJy
Oct 10th, 2006, 10:31 AM
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
lunchboxtheman
Oct 10th, 2006, 04:10 PM
for(int intCnt = cards.length - 1; intCnt >= 0; inCnt++)
should be:
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.)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.