# [Processing × Java] How to use arrays

1. How to make a simple array
2. How to make an array using classes
3. How to make an array with many elements
4. Example using an array

# 0. What is an array?

## 0-0. ** Array is simply **

The data is arranged in a row. The arrangement (order) is meaningful. Something like coordinates (3,10) that you often see is also a type of array.

## 0-1. ** Why use arrays **

The main reason is ** Because you can handle a large amount of data at once. ** ** is.

## 0-2. ** Array image **

Ranking team
1 Giant
2 DeNA
3 Hiroshima
4 Hanshin
5 Chunichi
6 Yakult

This table is the standings of the Professional Baseball Central League. The order and elements (teams) are connected and lined up in a row. This is the image.

# 1. ** How to make a simple array **

① Declaration ② Initial setting ③ Use Follow the procedure to create an array and use it.

Let's look at a simple example.

#### `string.java`

``````
//Declaration of array name and length(The name is numbers and the length is 7)
int[] numbers = new int;
//Definition of array elements
numbers = 2;
numbers = 4;
numbers = 6;
numbers = 8;
numbers = 10;

//Use of arrays
int a = numbers + numbers;
println(a);
``````
``````12
``````

This program is a program that adds the elements (contents) of an array and displays the result. Let's take a closer look at each step.

### ** ① Declaration **

``````//Declaration of array name and length(The name is numbers and the length is 7)
int[] numbers = new int;
``````

** Point **: Declaration method ** Data type [] Array name = new Data type [Array length] **

To explain with this example "I'm going to create an array now, but the ** data in the array ** is an integer. ** The name of the array ** is numbers, and ** the number of data in the array ** is 5! " I feel like.

### ** ② Initial setting **

``````//Definition of array elements
numbers = 2;
numbers = 4;
numbers = 6;
numbers = 8;
numbers = 10;
``````

** Put the contents in the array. ** ** This time, it is a method to ** individually determine the contents of the elements of the array **.

• It is difficult if there are many elements.

### ** ③ use **

``````//Use of arrays
int a = numbers + numbers;
println(a);
``````

Array elements are specified in the form of ** array name [index] **. The index is the order of the elements in the array. The index starts from 0, like 0,1,2 ...

# 2. ** How to create an array using classes **

The program below is a program that draws two bouncing balls. This time, I would like to modify this program.

``````//Declaration of object
Ball b1;
Ball b2;

void setup(){
size(600,400);
//Define an object
b1 = new Ball(50,50,7,5);
b2 = new Ball(400,50,-7,-5);
}

void draw(){
background(255);
//Use object b1
b1.display();
b1.move();
b1.edges();

//Use object b2
b2.display();
b2.move();
b2.edges();
}

//Define a class
class Ball{
//Variables to use(field)Declare
int x;
int y;
float xspeed;
float yspeed;

//Ball class constructor
Ball(int xpos,int ypos,float xvelocity,float yvelocity){
x = xpos;
y = ypos;
xspeed = xvelocity;
yspeed = yvelocity;
}

//Function to display the ball(Method)
void display(){
fill(234,159,217);
noStroke();
ellipse(x,y,30,30);
}

//Function to move the ball(Method)
void move(){
x += xspeed;
y += yspeed;
}

//Function to bounce the ball(Method)
void edges(){
if(x > width-15 || x < 15){
xspeed *= -1;
}
if(y > height-15 || y < 15){
yspeed *= -1;
}
}
}
``````

## 2-0. Create an array from the class

Create an array balls from the Ball class. There are two elements (contents) in the array.

``````//Array(Name and length)Declaration of
Ball[] balls = new Ball;

void setup(){
size(600,400);
//Define the elements of the array
balls = new Ball(50,50,7,5);
balls = new Ball(400,50,-7,-5);

}

void draw(){
background(255);
//Handle the first element of the array balls
balls.display();
balls.move();
balls.edges();

////Handle the second element of the array balls
balls.display();
balls.move();
balls.edges();
}

//Define a class
class Ball{
//Variables to use(field)Declare
int x;
int y;
float xspeed;
float yspeed;

//Ball class constructor
Ball(int xpos,int ypos,float xvelocity,float yvelocity){
x = xpos;
y = ypos;
xspeed = xvelocity;
yspeed = yvelocity;
}

//Function to display the ball(Method)
void display(){
fill(234,159,217);
noStroke();
ellipse(x,y,30,30);
}

//Function to move the ball(Method)
void move(){
x += xspeed;
y += yspeed;
}

//Function to bounce the ball(Method)
void edges(){
if(x > width-15 || x < 15){
xspeed *= -1;
}
if(y > height-15 || y < 15){
yspeed *= -1;
}
}
}
``````

Let's take a closer look at how to create an array from a class. The flow of creating an array does not change. In other words ** ① Declaration ** ** ② Initial setting ** ** ③ use ** It is a flow.

### ** ① Declaration **

``````//Declaration of array name and length(The name is balls and the length is 2)
Ball[] balls = new Ball;
``````

** Point **: Declaration method ** Class [] Array name = new class [Array length] **

To explain with this example "I'm going to make an array from now on, but I'll make that array based on the ** Ball class **. The ** array name ** is balls, and the ** number of data in the array ** is two! " I feel like.

### ** ② Initial setting **

``````//Define the elements of the array
balls = new Ball(50,50,7,5);
balls = new Ball(400,50,-7,-5);
``````

** Define the elements of the array. ** ** This time, it is a method to ** individually determine the contents of the elements of the array **.

• It is difficult if there are many elements. ..

### ** ③ use **

``````//Handle the first element of the array balls
balls.display();
balls.move();
balls.edges();

////Handle the second element of the array balls
balls.display();
balls.move();
balls.edges();
``````

Array elements are specified in the form of ** array name [index] **.

By the way, the elements of an array are objects. Objects have class fields (variables) and methods (functions). Objects use dots (.) To access their fields and methods. This time, it is used in the form of ** object.method (); **.

# 3. ** How to make an array with many elements **

Next, I would like to modify the program in the above array.

``````//Array(Name and length)Declaration of
Ball[] balls = new Ball;

void setup(){
size(600,400);
//Define the elements of the array
//Put it together in a simple form with a for loop.
for(int i = 0;i < balls.length;i++){
balls[i] = new Ball(random(width),random(height),random(-i,i),random(-i,i));
}
}

void draw(){
background(255);
//Use an array
//Put it together in a simple form with a for loop.
for(int i = 0;i < balls.length;i++){
balls[i].display();
balls[i].move();
balls[i].edges();
}
}

//Define a class
class Ball{
//Variables to use(field)Declare
int x;
int y;
float xspeed;
float yspeed;

//Ball class constructor
Ball(int xpos,int ypos,float xvelocity,float yvelocity){
x = xpos;
y = ypos;
xspeed = xvelocity;
yspeed = yvelocity;
}

//Function to display the ball(Method)
void display(){
fill(234,159,217);
noStroke();
ellipse(x,y,30,30);
}

//Function to move the ball(Method)
void move(){
x += xspeed;
y += yspeed;
}

//Function to bounce the ball(Method)
void edges(){
if(x > width-15 || x < 15){
xspeed *= -1;
}
if(y > height-15 || y < 15){
yspeed *= -1;
}
}
}
``````

Let's take a closer look at how to create an array with many elements from a class. The flow of creating an array does not change. In other words ** ① Declaration ** ** ② Initial setting ** ** ③ use ** It is a flow.

### ** ① Declaration **

``````//Declaration of array name and length(The name is balls and the length is 20)
Ball[] balls = new Ball;
``````

** Point **: Declaration method ** Class [] Array name = new class [Array length] **

### ** ② Initial setting **

``````//Define the elements of the array
//Put it together in a simple form with a for loop.
for(int i = 0;i < balls.length;i++){
balls[i] = new Ball(random(width),random(height),random(-i,i),random(-i,i));
}
``````

Instead of defining elements individually, we will use loops to efficiently define them.

### ** ③ use **

``````//Use an array
//Put it together in a simple form with a for loop.
for(int i = 0;i < balls.length;i++){
balls[i].display();
balls[i].move();
balls[i].edges();
}
``````

Array elements are specified in the form of ** array name [index] **. Instead of using individual elements, we will use loops to use them efficiently.

By the way, the elements of an array are objects. Objects have class fields (variables) and methods (functions). Objects use dots (.) To access their fields and methods. This time, it is used in the form of ** object.method (); **.

# 4. ** Example using array **

It is a program that increases the number of balls each time you click the mouse.

``````//Array declaration
Ball[] balls = new Ball;

//I want to change the total number of balls, so I create a variable for that
int total = 0;

void setup(){
size(600,400);
//Define the elements of the array
//Put together in a simple form with a for loop
for(int i = 0;i < balls.length;i++){
balls[i] = new Ball(random(width),random(height),random(-i,i),random(-i,i));
}
}

//Each time the mouse is pressed, the number of balls will increase
void mousePressed(){
total += 1;
}

void draw(){
background(255);
//Use an array
//Put together in a simple form with a for loop
//A total number of balls will be drawn
for(int i = 0;i < total;i++){
balls[i].display();
balls[i].move();
balls[i].edges();
}
}

//Define a class
class Ball{
//Variables to use(field)Declare
int x;
int y;
float xspeed;
float yspeed;

//Ball class constructor
Ball(int xpos,int ypos,float xvelocity,float yvelocity){
x = xpos;
y = ypos;
xspeed = xvelocity;
yspeed = yvelocity;
}

//Function to display the ball(Method)
void display(){
fill(234,159,217);
noStroke();
ellipse(x,y,30,30);
}

//Function to move the ball(Method)
void move(){
x += xspeed;
y += yspeed;
}

//Function to bounce the ball(Method)
void edges(){
if(x > width-15 || x < 15){
xspeed *= -1;
}
if(y > height-15 || y < 15){
yspeed *= -1;
}
}
}
``````

# Finally

Thank you for reading. We appreciate your opinions and suggestions in order to make the article even better.