[Processing × Java] How to use arrays

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

table of contents 0. What is an array?

  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[5];
//Definition of array elements
numbers[0] = 2;
numbers[1] = 4;
numbers[2] = 6;
numbers[3] = 8;
numbers[4] = 10;

//Use of arrays
int a = numbers[0] + numbers[4];
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[5];

** 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[0] = 2;
numbers[1] = 4;
numbers[2] = 6;
numbers[3] = 8;
numbers[4] = 10;

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

** ③ use **

//Use of arrays
int a = numbers[0] + numbers[4];
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[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 the array balls
  balls[0].display();
  balls[0].move();
  balls[0].edges();

  ////Handle the second element of the array balls
  balls[1].display();
  balls[1].move();
  balls[1].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[2];

** 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[0] = new Ball(50,50,7,5);
balls[1] = 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 **.

** ③ use **

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

////Handle the second element of the array balls
balls[1].display();
balls[1].move();
balls[1].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[20];

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[20];

** 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[20];

//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.

Recommended Posts

[Processing × Java] How to use arrays
[Processing × Java] How to use variables
[Processing × Java] How to use the loop
[Processing × Java] How to use the class
[Processing × Java] How to use the function
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use removeAll ()
How to use Java Map
How to use Java variables
How to use arrays (personal memorandum)
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
[Processing × Java] How to use loop 2 --- Nested structure, coordinate conversion
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Easy-to-understand explanation! ] How to use Java instance
Studying how to use the constructor (java)
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
How to use Java Scanner class (Note)
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
How to use Map
How to use rbenv
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Dozer.mapper
How to use Gradle
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]
How to use the replace () method (Java Silver)
How to use scope and pass processing (Jakarta)