You go to Ebay or an online store, and you buy a book, a computer, and a shirt. How is the computer able to write a pointer and refer to that which you bought. It didn't know what you were planning to buy. There aren't hundreds of programmers hired by ebay to quickly write up a code for each user, writing "create a variable for shirt, etc.". It must be that it has the ability to create a reference (pointer) to each individual object. How? Besides the fact, how is it able to store your records. Ebay has hundreds of people on record, and millions of pieces of merchandise. How is it able to point to them all, and use each one individually?
Let's say that you have a hundred students in the class you are teaching. You have a piece of code (method) that will calculate their grades for you. Are you going to have to write the same code for each student
calculate grades for john
That's ridiculous.
calculate grades for sue
calculate grades for dave
etc.
The answer to both of these questions are data structures. Data structures loosely defined are lists, and containers that store similar things. For example, you can have a data structure that stores all of the students of your class. Whenever you want to treat the whole class as one, you make use of this "list". Also, every thing that you buy goes in a list, and therefore, ebay only needs to make one object for you. A list. Then, whenever you buy something they add it to your list.
There are three ways to create a data structure, and get this list. The first way is known as an array. An array is a built-in data structure, that comes automatically with Java. It is the easiest to use, and the best kind if you want to pick out one individual thing out of the list. An array can be made of anything. It can be made of primitives (simple things in Java that are not an object. They can only do one thing. Like an int, is a number, it can do nothing else), specific objects (like in my example, you can make an array of students), generic objects (you can make an array of objects, and store different objects in each one), or even of other arrays. You can refer to the list, or to a specific person in the list. For example, lets say that you have an array of students called AllOfMyStudents. If you want to refer and use the whole array, you refer to it as AllOfMyStudents. (An array is written also as AllOfMyStudents[]). But, if you want to refer to John, and John is the 10th student in your array, then the word AllOfMyStudents[10] is referring to John. (Actually Arrays start counting with number zero, so in reality AllOfMyStudents[9] would be John). In my example where you want to calculate the grades for all of your students in your class, and if all of your students are in an array, you can do it using a Loop (see Control Statements ), and it will be easier, and only take a few lines. This is what the pseudo code looks like.
Notice, that it makes no difference if you have 10 students, or ten thousand students, if they are all in an array this piece of code will run the same way.
1. start the loop to continue again and again until the condition stops
2. create a counter, start the counter at zero, and call the counter studentNumber
3. while there are more students keep on doing this, when there are no more students continue after loop
4. take the student[] (remember, that the arrayName with braces refer to the whole array
5. take the student[studentNumber] (remember, that the arrayName with a number in the braces refer to an individual student, and here studentNumber is a number variable, that keeps on going up by one)
6. do whatever code you need to do to calculate the grade of student[studentNumber]
7. add one to the variable of studentNumber.
Even though arrays are fast and easy to access and use, there's one big problem. Once you create the array, it is technically impossible to add to the number, or insert something in the middle. Therefore, there are two other kinds of data structure solutions. One involves creating your own kind of data structure (remember, an array is built into the Java language). This is an advanced topic, that has college classes, and many different books just on it, but in short, think about a chain. A chain is one object that is made up of many different links. Each link is a thing by itself, and it also "points" to the next link. The same is true with a linked list. You create the list, and if you want to go through the list, you just run from one link to the next. There are many different kinds, and the "links" can hold whatever information you want, depending on the way that you created it. Meaning, you can make a linked list of students, or of numbers, since you write the code of how it works.
The third way involves using a premade list from the Java library. As Java holds many premade classes, some of their premade classes are premade lists, that you can just bring in and use (see the Java API Library section). These are very easy to use, but since they are made by someone else, you have to use them with the rules that they come with.