|
-
Sep 26th, 2006, 02:06 AM
#1
Thread Starter
Lively Member
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
-
Sep 26th, 2006, 08:11 AM
#2
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
-
Sep 26th, 2006, 10:11 AM
#3
Re: Array question.
Is it possible to have an array that's empty but not null ie array.length==0
-
Sep 26th, 2006, 10:23 AM
#4
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
-
Sep 26th, 2006, 10:24 AM
#5
Lively Member
-
Sep 26th, 2006, 10:49 AM
#6
Lively Member
Re: Array question.
for the question #2, I did exactly what you said and I got an error message "Illegal start of expression".
-
Sep 26th, 2006, 11:11 AM
#7
Re: Array question.
 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
-
Sep 26th, 2006, 12:05 PM
#8
Lively Member
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++)
-
Sep 26th, 2006, 12:19 PM
#9
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
-
Sep 26th, 2006, 12:28 PM
#10
Lively Member
-
Oct 9th, 2006, 08:13 AM
#11
Lively Member
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.
-
Oct 9th, 2006, 10:38 AM
#12
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.
-
Oct 10th, 2006, 09:26 AM
#13
Lively Member
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?
-
Oct 10th, 2006, 10:31 AM
#14
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
-
Oct 10th, 2006, 04:10 PM
#15
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|