[Java] [Processing x Java] How to use array

8 minute read

This article is for understanding the structure of the program through Processing. This time I will write about arrays.

table of contents

  1. What is an array?
  2. How to make a simple array
  3. How to create an array using classes
  4. How to create an array with many elements
  5. Example using array

0. What is an array?

0-0. **The array is simply **

It is a series of data. The arrangement (order) has meaning. The one we often see like coordinates (3,10) is also a type of array.

0-1. Why use an array

The main reason is **Because a large amount of data can be handled collectively. ** is.

0-2. Array image

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

This table is the standings of the professional baseball series. The order and elements (teams) are tied together in a line. This is the image.

1.How to make a simple array

① Declaration ② Initial setting ③ Use In this procedure, we will create an array and use it.

Let’s take a look at a simple example.

string.java


// array name and length declaration (name is numbers and length is 7)
int[] numbers = new int[5];
// array element definition
numbers[0] = 2;
numbers[1] = 4;
numbers[2] = 6;
numbers[3] = 8;
numbers[4] = 10;

// using arrays
int a = numbers[0] + numbers[4];
println(a);
12

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

###** ① Declaration**

.java


// array name and length declaration (name is numbers and length is 7)
int[] numbers = new int[5];

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

In this example, “I’m going to create an array, 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 that.

###② Initial setting

.java


// array element definition
numbers[0] = 2;
numbers[1] = 4;
numbers[2] = 6;
numbers[3] = 8;
numbers[4] = 10;

** Populate the array. ** This time, it is a method of determining the contents of array elements. *It is difficult if there are many elements.

###③ Use

.java


// using arrays
int a = numbers[0] + numbers[4];
println(a);

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

2.How to create an array using a class

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

.java


// object declaration
Ball b1;
Ball b2;

void setup(){
  size(600,400);
  // define the 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 the class
class Ball{
  // Declare the variable (field) to use
  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 (method) to display the ball
  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 (method) to bounce the ball
  void edges(){
    if(x> width-15 || x <15){
      xspeed *= -1;
    }
    if(y> height-15 || y <15){
      yspeed *= -1;
    }
  }
}

2-0. Creating an array from a class

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

.java


// array (name and length) declaration
Ball[] balls = new Ball[2];

void setup(){
  size(600,400);
  // define the elements of the array
  balls[0] = new Ball(50,50,7,5);
  balls[1] = new Ball(400,50,-7,-5);

}

void draw(){
  background(255);
  // handle the first element of array balls
  balls[0].display();
  balls[0].move();
  balls[0].edges();

  //// handle the second element of array balls
  balls[1].display();
  balls[1].move();
  balls[1].edges();
}

// define the class
class Ball{
  // Declare the variable (field) to use
  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 (method) to display the ball
  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 (method) to bounce the ball
  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 making an array does not change. That is ** ① Declaration ** ** ② Initial setting ** ③ Use It is the flow.

###** ① Declaration**

.java


// array name and length declaration (name is balls and length is 2)
Ball[] balls = new Ball[2];

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

In this example, “I’m going to create an array from now on, but that array is based on the Ball class. The **name of the array is balls and the number of data in the array is 2! “ I feel like that.

###② Initial setting

.java


// define the elements of the array
balls[0] = new Ball(50,50,7,5);
balls[1] = new Ball(400,50,-7,-5);

Defines the elements of the array. ** This time, it is a method of **determining the contents of array elements. *It is difficult if there are many elements. ..

###③ Use

.java


// handle the first element of array balls
balls[0].display();
balls[0].move();
balls[0].edges();

//// handle the second element of array balls
balls[1].display();
balls[1].move();
balls[1].edges();

The elements of the array are specified in the format array name [index ].

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

3.How to create an array with many elements

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

.java


// array (name and length) declaration
Ball[] balls = new Ball[20];

void setup(){
  size(600,400);
  // define the elements of the array
  Put together in a simple form with //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 together in a simple form with //for loop.
  for(int i = 0;i <balls.length;i++){
    balls[i].display();
    balls[i].move();
    balls[i].edges();
  }
}

// define the class
class Ball{
  // Declare the variable (field) to use
  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 (method) to display the ball
  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 (method) to bounce the ball
  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 making an array does not change. That is ** ① Declaration ** ** ② Initial setting ** ③ Use It is the flow.

###** ① Declaration**

.java


// array name and length declaration (name is balls and length is 20)
Ball[] balls = new Ball[20];

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

###② Initial setting

.java


// define the elements of the array
Put together in a simple form with //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 the elements individually, use a loop to efficiently define them.

###③ Use

.java


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

The elements of the array are specified in the format array name [index ]. Instead of using individual elements, use loops efficiently.

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

4.Example using array

It is a program in which the ball increases with each click of the mouse.

.java


// array declaration
Ball[] balls = new Ball[20];

// I want to change the total number of balls, so 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));
  }
}

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

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

// define the class
class Ball{
  // Declare the variable (field) to use
  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 (method) to display the ball
  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 (method) to bounce the ball
  void edges(){
    if(x> width-15 || x <15){
      xspeed *= -1;
    }
    if(y> height-15 || y <15){
      yspeed *= -1;
    }
  }
}

Finally

Thank you for reading. We would appreciate your comments and suggestions for improving the article.

*This article is based on Daniel Shefman’s youtube “Coding Train”.