I don't understand what I don't understand, so I can't explain it well, but if you can tell me what I can't tell you with a supplementary request, it will be very helpful. Thank you in advance.
A problem with Java silver SE8 array instances and element values.
You can choose the execution result of the next program from the choices.
public class Item {
String name;
int price = 100;
}
public class Main {
public static void main (String [] args) {
Item [] items = new Item [3];
int total = 0;
for (int i = 0;i<items.length;i ++) {
total + = items [i] .price;
}
System.out.println (total);
}
}
The correct answer is "An exception is thrown at run time", and part of the commentary is "It's easy to get confused by beginners. Is mentioned. "
objects can usetotal + = object name.price;
to assign the price of each object to total. + = Does it mean that the array variable name [] .price;assigns the price of each element of the array to total? That would seem to be an exception in any case, but please give me an example of a program that uses a statement like this (array variable name [].
).
-
Answer # 1
-
Answer # 2
I understand that the price of each object can be substituted into total with total + = object name.price;if it is an
object, but total + = array variable name [] .price;Does that mean that the price for each element of the array is assigned to total?
In this case, array elements are also objects.
It's the same as writing:Item item = items [i]; total + = item.price;
Related articles
- i don't understand the java problem
- java - i don't understand the problem of double for statement with array
- java - i want to send data as intent from fragment in androidstudio mainactivity → subactivity
- css in html,css:form tag is no longer reflected if you can understand java a little, please give me instructions
- php - i want to solve tle of array problem
- java - array problems with methods
- c++ - i solved the problem with atcoder, but i don't understand the cause of the segmentation fault
- i don't really understand the java environment
- java - about array code
- java - i can't understand the transition relationship of html files in spring boot
- [java] i don't understand that upcasts are automatically cast without explicit casts
- pass by value of java array variable
- [java] handling of array elements and null
- java - an error occurs in the element part of the array and it does not resolve
- [c++] i don't understand the cause of atcoder's problem being re
- java - i have a question about a problem with interface
- vuejs - i don't understand why the array comes from mapget
- java - i want to pass data with intent when another application is started with android, but if the application to be started is
- get the class of java array
- java - although there is no problem in transition from html to jsp, insert is not reflected
As you can see if you debug, at this point you have just three boxes to put the
Item
class, and the contents of each box are empty.(Primitive types such as int contain initial values.)
In other words, every element of the array is null until you assign an instance to each element.