I\'m writing a Java program for an assignment and having someissues. I have an a
ID: 3614276 • Letter: I
Question
I'm writing a Java program for an assignment and having someissues.I have an array of integers that is created and initialized with asize that is passed into the function. A Part of theassignment is to determine the size of the array itself, which I'vecompleted using the .length as well as to find how many values areactually in the array so that arrayoutofboundexceptions can beavoided.
Ex:
Create an array of 10 int's
Get values to insert into array from user.
Let's say user enters 3 values, leaving the remaining 7 places inthe array blank.
How can I find out how many values the array actually holds (3)?? I know that the default values are 0
s but I can't loop to catch 0's as the user may input this value atwhich point everything post that user inputted value of 0 is notcounted. All I can think of is to initialize the array withsome random large number and scan for that while interating acounter. Is there a better way?
D
Explanation / Answer
please rate - thanks without the code it's difficult to say but your idea will work count the numbers as you put them in the array, and check that thecounter, element up to, is less than, or equal to the length of thearray