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?
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.
The main reason is ** Because you can handle a large amount of data at once. ** ** is.
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.
① 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 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.
//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 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 ...
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;
}
}
}
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 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.
//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 **.
//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 (); **.
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 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] **
//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 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 (); **.
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;
}
}
}
Thank you for reading. We appreciate your opinions and suggestions in order to make the article even better.
Recommended Posts