Data Structures-Size Method


#1

So for the "size method" the "int" represents how many elements are stored (sample code below). Why can't the int just be 0 instead of i=0? Probably a stupid question, I just don't understand why we cant write for (int=0) instead of for (int i=0). The code we are using hasn't used int abcdefgh so why i?

Thanks for your help!

Initialization: int variable i is set to 0 which is the first index of an ArrayList.

for (int i = 0; i < quizGrades.size(); i++) {

System.out.println( quizGrades.get(i) );

}


#2

Well the amount of good variable names is not endless and counting variables don't need to have fancy names as they only serve one purpose in the for loop and only exist in the scope of this for loop. So it is popular to use one letter counting variables and as you mainly count up from 0 to how-many-loops-you-want-to-do in steps of 1, so it say in integer steps it's quite popular to use i (for integer). That's pretty much it for reasons why to use i short, disposable and anyone knows it contains an integer.

As you ask for this: int=0
I need to ask are you aware of what datatypes are and why you use them?

The idea is pretty simple you want space in the memory in javaScript you would just order this by var and in python just by using a name but Java is not a scripting language so it is more powerful which has it's price. Meaning that you need to be more specific. Let's say your computer's memory is build of blocks of let's say 64bit meaning 64 times either 0 or 1. Then using

datatype name;

request: "Give me one of these blocks and let me reference it by name"
Now let's say you want to store 65 there, what you do is translate it to binary numbers and store it there:

00...0001000001

the problem is now if you want to read it again how do you know what that means? Yes it could be the integer 65, but it could also be the number encoding the ascii letter A or it could be a floating point number. And that is where you need datatypes with int you say: "Give me one of this blocks and treat it's input like an integer value". So int is not a variable name but the type of the variable.
As said Java is more powerful than a scripting language as it operates closer to the OS (in it's own virtual machine) but this comes with the price that one now has to deal with such things.

I hope this was not too much and too confusing :smile: