Learn Java Coding

Everything You Have To Know About Java

Arrays

pre-knowledge

Definition

An array is like a collection of things.

Theory

An array are boxes, in which you save data:

array java

As you can see the boxes are empty. You create an empty array just by commanding how many boxes you need

This is what displayed picture looks like in Java
Boxes [] emtpyBoxes = new Boxes[3];

(In the this case 3)

To fill an array, you take the name of the array and add an object on a specific index place.

arrayName [index] = object;

Example


array collection java

code of the above image

Final code click here.

Explanation ▽

To create above collections

  1. First create an object Chair, e.g.:
    package Array;
    
    public class Chair {
    	
    	String color;
    	
    	public Chair(String color) {
    		this.color = color;
    	}
    
    }

if you don’t understand:

2. Now, also create an object Table e.g.:

package Array;

public class Table {
	
	String color;
	
	public Table(String color) {
		this.color = color;
	}

}

if you don’t understand:

3. Now we create a MAIN CLASS. In this class you bring all tables and chairs alive.

  • 3 Chair objects
  • 2 Table objects
package Array;

public class Array {

	void start() {
		Chair blueChair = new Chair("blue");
		Chair redChair = new Chair("red");
		Chair yellowChair = new Chair("yellow");
		
		Table blueTable = new Table("blue");
		Table blackTable = new Table("black");
	}
	
	public static void main(String[] args) {
		new Array().start();

	}

}

Now you could test the color of the chairs and tables using a print-statement

4. For simplicity, we create two more objects; One for the collection of chairs and one for the collection of tables.

  • chairCollection:
    package Array;
    
    public class ChairCollection {
    	
    }
    	
  • tableCollection:
    package Array;
    
    public class TableCollection {
    
    }

5. Because we want to add the chairs and tables that we have created in the main class to our collections (which we also are going to create in the main class), we are going to create 2 arrays.

  • chairCollection:
    package Array;
    
    public class ChairCollection {
    	Chair[] collection;
    	int i;
    	
    	public ChairCollection() {
    		collection = new Chair[3];
    		i = 0;
    	}
    }
  • tableCollection:
    package Array;
    
    public class TableCollection {
    
    	Table[] collection;
    	int i;
    	
    	public TableCollection() {
    		collection = new Table[2];
    		i = 0;
    	}
    
    }

6. Because we want to add chairs and tables to our collections, we have to create an add method

  • chairCollection:
    package Array;
    
    public class ChairCollection {
    	Chair[] collection;
    	int i;
    	
    	public ChairCollection() {
    		collection = new Chair[3];
    		i = 0;
    	}
    	
    	void add (Chair newChair) {
    		collection[i] = newChair;
    		i++;
    	}
    }
  • tableCollection:
    package Array;
    
    public class TableCollection {
    
    	Table[] collection;
    	int i;
    	
    	public TableCollection() {
    		collection = new Table[2];
    		i = 0;
    	}
    	
    	
    	void add (Table newTable) {
    		collection[i] = newTable;
    		i++;
    	}
    }

7. Now we bring our collections to live in the main class.

And we add the objects to the collections

This results in a main class with:

  • 2 arrays of:
    • 3 chair objects
    • 2 table objects
package Array;

public class Array {

	void start() {
		Chair blueChair = new Chair("blue");
		Chair redChair = new Chair("red");
		Chair yellowChair = new Chair("yellow");
		
		Table blueTable = new Table("blue");
		Table blackTable = new Table("black");
		
		ChairCollection chairCollection = new ChairCollection();
		TableCollection tableCollection = new TableCollection();
		
		chairCollection.add(blueChair);
		chairCollection.add(redChair);
		chairCollection.add(yellowChair);
		
		tableCollection.add(blueTable);
		tableCollection.add(blackTable);
	}
	
	public static void main(String[] args) {
		new Array().start();

	}

}

8. How to get information from these two arrays?

You can do it like this; for example, if you want to want to show which color the first chair is in your chair collection:

package Array;

public class Array {

	void start() {
		Chair blueChair = new Chair("blue");
		Chair redChair = new Chair("red");
		Chair yellowChair = new Chair("yellow");
		
		Table blueTable = new Table("blue");
		Table blackTable = new Table("black");
		
		ChairCollection chairCollection = new ChairCollection();
		TableCollection tableCollection = new TableCollection();
		
		chairCollection.add(blueChair);
		chairCollection.add(redChair);
		chairCollection.add(yellowChair);
		
		tableCollection.add(blueTable);
		tableCollection.add(blackTable);
		
		System.out.println(chairCollection.collection[0].color);
	}
	
	public static void main(String[] args) {
		new Array().start();

	}

}

But you can also include this in the collection classes:

String getColor(int index) {
	return collection[index].color;
}

Result of the classes:

  • chairCollection:
    package Array;
    
    public class ChairCollection {
    	Chair[] collection;
    	int i;
    	
    	public ChairCollection() {
    		collection = new Chair[3];
    		i = 0;
    	}
    	
    	void add (Chair newChair) {
    		collection[i] = newChair;
    		i++;
    	}
    	
    	String getColor(int index) {
    		return collection[index].color;
    	}
    }
  • tableCollection:
    package Array;
    
    public class TableCollection {
    
    	Table[] collection;
    	int i;
    	
    	public TableCollection() {
    		collection = new Table[2];
    		i = 0;
    	}
    	
    	
    	void add (Table newTable) {
    		collection[i] = newTable;
    		i++;
    	}
    	
    	String getColor(int index) {
    		return collection[index].color;
    	}
    }

Now you can ask your main class if you can get the colors of certain elements, which is preferred:

package Array;

public class Array {

	void start() {
		Chair blueChair = new Chair("blue");
		Chair redChair = new Chair("red");
		Chair yellowChair = new Chair("yellow");
		
		Table blueTable = new Table("blue");
		Table blackTable = new Table("black");
		
		ChairCollection chairCollection = new ChairCollection();
		TableCollection tableCollection = new TableCollection();
		
		chairCollection.add(blueChair);
		chairCollection.add(redChair);
		chairCollection.add(yellowChair);
		
		tableCollection.add(blueTable);
		tableCollection.add(blackTable);
		
		System.out.println("The color of this chair is: " + chairCollection.getColor(2));
	}
	
	public static void main(String[] args) {
		new Array().start();

	}

}

 

 

GO TO NEXT

Next Post

Previous Post

© 2019 Learn Java Coding