Java Array of Structure
Greetings! I am new to JAVA. I'm coming over from VB .NET. In VB I was introduced to an array of a structure. It came in handy and I was wondering if it was possible in JAVA. I've tried to find an answer via Google, but can't seem to understand most of the potential examples. My goal is to load several pieces of data of different types into an array and be able to index them all by 1 number per group. So for a simple example I would use firstName, lastName, and age. These would be loaded into the array. I would index them all with the same number. array.firstName, array.lastname, array.age. I'm sorry the syntax is probably incorrect for JAVA, but hopefully you understand what I'm getting at. Any help would be greatly appreciated!!!
Welcome to Java.
Java has many options to manage information in groups or arrange in orders. Let's take the same example you have given, you need to manage firstName, lastName, and age. So in typical object oriented style, you would create a Person class with these information as attributes. Then each object of this class would represent a person.
Suppose you have many such person objects. Now you need to find a way to manage these objects. In Java, the collection framework does the trick for you.
Java collection is a framework with a group of classes and interfaces that help in manipulating objects as a single unit. Collection classes are commonly used Java and belong to the java.util package. For more details on Collections, go to Trail: Collections (The Java™ Tutorials). You may also look at 'Arrays' in java (Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics))
Ok, lets get back to your specific need. If you want to have an ordered list of information - such as person objects-, you may use an ArrayList. ArrayList is ordered and allows positional access (acess information using index) .
(Note :ArrayList is the concrete implementation of the List interface)
We know this is a lot of stuff but once you get an idea of the Collection framework, you will apprecite its usefulness in Java.
Do not hesitate to ask questions, if you come across anything that needs little more explanation.
Hope this helps!