+ 1
Question about array in java
I have a question. When i declare a Array like this int[] myNumbers = new int[5]; that means i didnt put and i didnt declare any values in the five reserved places yet and the values is null in the five places until i do this int[ ] myNumbers = {5,6,7,8,}; am i right ?
9 Answers
+ 3
For an int yes. An empty array is all 0's.
For float/doubles it would be 0.0
For booleans it would be false
For chars it would be '\u0000'
For objects it would be null.
Whatever the default value is.
+ 5
The places are not null.
= new int[5] will initialize the default value (for ints its 0) to all 5 elements. So, it is an empty array. By doing so you'd have this array: {0,0,0,0,0}
= {5,6,7,8} will initialize the values accordingly.
This method is useful if you already know the values when creating the array.
You can also do:
= new int[]{5,6,7,8};
This is the same thing, not writing this is just syntax sugar since the compiler still creates a new array for you anyway.
+ 2
so as long i don't declare any values in the array so the empty places value is 0 ?
+ 2
*Assuming you know that the loop will run 5 times*
You've printed the arrays length inside the loop.
for(....)
{
This is what's inside the loop. Everything in here will be looped (or ran by the loop).
So: '{}' is called the 'block' or 'scope', and everything in the brackets is what the loop contains. (What will be looped)
}
Just like how this will print 'a' five times:
for(int i = 0; i < 5; i++)
{
System.out.println(" 'a' ");
}
This is what you want:
System.out.println(arr.length);
for(int i = 0; i < 5; i++)
{
// other stuff in here to loop
}
+ 2
So it doesn't matter if you use the variable in the loop or not, whatever is in the block of the loop is what will be looped.
Here's another example that should be helpful:
System.out.println("Start!");
for(int i = 0; i < 5; i++)
{
System.out.println(i);
System.out.println("next!");
}
System.out.println("Finished!");
~~Output~~
Start!
0
next!
1
next!
2
next!
3
next!
4
next!
Finished!
+ 2
Yup! the 'hahaha' is in it's own block of nothingness. Only the first block after your loop statement is what's looped.
So for other loops:
while(condition)
{
//this is looped
}
do
{
//this is looped
}while(condition);
+ 1
Thank you very much for the answer. I have a another question.
public class Program {
public static void main(String[] args) {
int[] arr = new int[3]; //array is int type and holds 3 elements
for (int i=0; i<5; i++) {
System.out.println(arr.length);
}
} // main
in this code I declared a array without giving it any values for the empty places. and then I wrote a loop and then I wanted to print the length ( 3).
I didn't initialize arr and didn't give the array any value like this arr [i]=i. and when I was printing out I didn't make any connection between arr and the variable "i" like this System.out.println (arr [i]);
my question is why is the out put 3 five times ? why isn't the out put only 3 once ? I didn't do any connection or relationship between arr and the variable "i" how can the loop still affect and influence in arr ?
+ 1
Thank your very much for the clear perfect answer. i understood now why the .length is looped 5 times.
Regardless i print
for(int i = 0; i < 5; i++)
{
System.out.println(" 'a' ");
}
or print
for(int i = 0; i < 5; i++)
{
System.out.println(arr.length);
}
so every thing inside the loop will prints 5 times.
but if i do like this
for (int i=0; i<5; i++) {
// initialize arr
System.out.println(arr.length);
}
{
System.out.print("hahaha");
}
That means the "hahaha" will be printed only once because its outside the loop block. am i right ?
+ 1
Thank you very much for the answers âșđ