UE 2I002 (ex LI230) : éléments de programmation par objets avec Java
TD3 - Tableau
Juliana Silva Bernardes
http://www.lcqb.upmc.fr/julianab/teaching/JAVA/
‣Tableau d'eléments de type simple
‣Tableau de tableaux (deux dimensions)
‣Tableau d’objets à une dimension
Sumary
3
Tableau
‣ Un tableau est une structure de données contenant un groupe d'éléments tous du même type.
‣ Le type des éléments peut être un type primitif ou une classe
Tableau d'eléments de type simple
int tabInt [];
‣ Declaration de tableau avec un type primitif
int [] tabInt;
char [] tabChar;
char tabChar [];
‣ Un tableau peut être initialisé :
int tabInt [] = {1,2,3,4};
char tabChar [] = {‘a’, ‘b’, ‘c’ }
‣ Pour allouer l’espace nécessaire au tableau il faut utiliser new int [] tabInt = new int [5];
5
Tableau d'eléments de type simple
‣ Pour allouer l’espace nécessaire au tableau il faut utiliser new int [] ti = new int [10];
ti[0] = 3;
3
ti[4] = 7;
ti[10] = 8; NullPointerException 7
‣ Un tableau possède un attribut length qui permet de connaître le nombre d’éléments d’un tableau.
ti.length vaut 10.
Tableau d'eléments de type simple
‣ Remplir un tableau avec des valeurs aleatoires
int [] ti = new int [10];
//valeurs aleatoires [0, 10]
for (int i = 0; i < ti.length; i ++){
‣ Remplir un tableau avec des valeurs numeriques 1 à 10 int [] ti = new int [10];
//valeurs aleatoires [0, 10]
for (int i = 0; i < ti.length; i ++){
ti[i] = i;
}
7
Tableau de tableaux (deux dimensions)
Tableau de tableaux (deux dimensions)
‣ Dans un tableau le nombre de crochets indique le nombre de dimensions du tableau.
int t2[][] = new int[5][10]
t2[4][3] = 8;
8
9
Tableau de tableaux (deux dimensions)
‣ Un tableau à plusieurs dimensions peut être initialisé : int t21[][] = {{1, 2, 3},
{4, 5, 6}};
Tableau de tableaux (deux dimensions)
‣ Remplir un tableau de deux dimension (3x4) avec des valeurs consécutives commençant par 0
!
!
int n = 0;
for( int i = 0; i< tab2.length; ++i){
for( int j = 0; j< tab2[i].length; ++j){
tab2[i][j] = n++;
} }
int [][] tab2 = new int [3][4];
11
Tableau de tableaux (deux dimensions)
‣ Toutes les lignes d’un tableau à 2 dimensions n’ont pas forcément le même nombre d’éléments :
int t22[][] ;
t22 = new int[5][];
for( int i = 0; i< t22.length; ++i){
t22[i]= new int [i+1];
}
Tableau de tableaux (deux dimensions)
‣ Pour acceder les valeurs d’un tableau à 2 dimensions.
for( int i = 0; i< t22.length; ++i){
for( int j = 0; j< t22[i].length; ++j){
System.out.print(t22[i][j]);
} }
13
Tableau de tableaux (deux dimensions)
‣ Exemple : Declarer et inicialiser un tableau pour representer la figure
* * * *
* * *
* *
*
Tableau de tableaux (deux dimensions)
‣ Exemple : Declarer et inicialisé un tableau pour representer la figure char tFigure[][] = new char[4][];
tFigure
0 1
* * * *
* * *
* *
*
15
Tableau de tableaux (deux dimensions)
‣ Exemple : Declarer et inicialisé un tableau pour representer la figure char tFigure[][] = new char[4][];
//Inicialisation
for( int i = 0; i< tFigure.length; i++){
tFigure[i]= new char [tFigure.length - i];
for( int j = 0; j< tFigure[i].length; j++){
tFigure[i][j] = ‘*’;
}
tFigure
0 1 2 3
i=0
* * * *
* * *
* *
*
Tableau de tableaux (deux dimensions)
‣ Exemple : Declarer et inicialisé un tableau pour representer la figure char tFigure[][] = new char[4][];
//Inicialisation
for( int i = 0; i< tFigure.length; i++){
tFigure[i]= new char [tFigure.length - i];
for( int j = 0; j< tFigure[i].length; j++){
tFigure[i][j] = ‘*’;
}
tFigure
0 1
i=0
* * * *
j=0 j=1 j=2 j=3
* * * *
* * *
* *
*
17
Tableau de tableaux (deux dimensions)
‣ Exemple : Declarer et inicialisé un tableau pour representer la figure char tFigure[][] = new char[4][];
//Inicialisation
for( int i = 0; i< tFigure.length; i++){
tFigure[i]= new char [tFigure.length - i];
for( int j = 0; j< tFigure[i].length; j++){
tFigure[i][j] = ‘*’;
}
tFigure
0 1 2 3
i=1
* * * *
Tableau de tableaux (deux dimensions)
‣ Exemple : Declarer et inicialisé un tableau pour representer la figure char tFigure[][] = new char[4][];
//Inicialisation
for( int i = 0; i< tFigure.length; i++){
tFigure[i]= new char [tFigure.length - i];
for( int j = 0; j< tFigure[i].length; j++){
tFigure[i][j] = ‘*’;
}
tFigure
0 1
i=1
* * * *
* * *
19
Tableau de tableaux (deux dimensions)
‣ Exemple : Declarer et inicialisé un tableau pour representer la figure char tFigure[][] = new char[4][];
//Inicialisation
for( int i = 0; i< tFigure.length; i++){
tFigure[i]= new char [tFigure.length - i];
for( int j = 0; j< tFigure[i].length; j++){
tFigure[i][j] = ‘*’;
}
tFigure
0 1 2 3
i=3
* * * *
* * *
* *
*
Tableau d’objets à une dimension
21
Tableau d’objets à une dimension
‣ Exemple : Declarer et initialiser un tableau d'objets
public class Point2D {
// variables d’instance private int x;
private int y;
//Constructeurs
public Point2D() {this(0, 0);}
public Point2D(int x, int y) {this.x = x; this.y = y;}
}
public class TestPoint2D {
public static void main(String [] args) { Point2D [] tabPoints = new Point2D[3];
}
tabPoints
0 1 2
Tableau d’objets à une dimension
public class Point2D {
// variables d’instance private int x;
private int y;
//Constructeurs
public Point2D() {this(0, 0);}
public Point2D(int x, int y) {this.x = x; this.y = y;}
}
public class TestPoint2D {
public static void main(String [] args) { Point2D [] tabPoints = new Point2D[3];
Point2D p1 = new Point2D();
!
}
x=0 y=0
tabPoints
0
p1
23
Tableau d’objets à une dimension
public class Point2D {
// variables d’instance private int x;
private int y;
//Constructeurs
public Point2D() {this(0, 0);}
public Point2D(int x, int y) {this.x = x; this.y = y;}
}
public class TestPoint2D {
public static void main(String [] args) {
Point2D [] tabPoints = new Point2D[3];
Point2D p1 = new Point2D();
tabPoints[0] = p1;
!
}
x=0
tabPoints y=0
0 1 2
p1
Tableau d’objets à une dimension
public class Point2D {
// variables d’instance private int x;
private int y;
//Constructeurs
public Point2D() {this(0, 0);}
public Point2D(int x, int y) {this.x = x; this.y = y;}
}
public class TestPoint2D {
public static void main(String [] args) {
Point2D [] tabPoints = new Point2D[3];
Point2D p1 = new Point2D();
tabPoints[0] = p1;
tabPoints[1] = new Point2D(3, 5);
}
x=0
tabPoints y=0
0 1 2
p1
x=3 y=5
25
Tableau d’objets à une dimension
public class Point2D {
// variables d’instance private int x;
private int y;
//Constructeurs
public Point2D() {this(0, 0);}
public Point2D(int x, int y) {this.x = x; this.y = y;}
}
public class TestPoint2D {
public static void main(String [] args) {
Point2D [] tabPoints = new Point2D[3];
Point2D p1 = new Point2D();
tabPoints[0] = p1;
tabPoints[1] = new Point2D(3, 5);
tabPoints[2] = new Point2D(6, 8);
}
x=0
tabPoints y=0
0 1 2
p1
x=3 y=5
x=6 y=8
Tableau d’objets à une dimension
public class Point2D {
// variables d’instance private int x;
private int y;
//Constructeurs
public Point2D() {this(0, 0);}
public Point2D(int x, int y) {this.x = x; this.y = y;}
}
public class TestPoint2D {
public static void main(String [] args) {
Point2D [] tabPoints = new Point2D[3];
Point2D p1 = new Point2D();
tabPoints[0] = p1;
tabPoints[1] = new Point2D(3, 5);
tabPoints[2] = new Point2D(6, 8);
p1.setX(2);
}
x=2
tabPoints y=0
0 1 2
p1
x=3 y=5
27
Tableau d’objets à une dimension
public class Point2D {
// variables d’instance private int x;
private int y;
//Constructeurs
public Point2D() {this(0, 0);}
public Point2D(int x, int y) {this.x = x; this.y = y;}
}
public class TestPoint2D {
public static void main(String [] args) {
Point2D [] tabPoints = new Point2D[3];
Point2D p1 = new Point2D();
tabPoints[0] = p1;
tabPoints[1] = new Point2D(3, 5);
tabPoints[2] = new Point2D(6, 8);
p1.setX(2);
System.out.println(tabPoints[0].getX());
}
x=2
tabPoints y=0
0 1 2
p1
x=3 y=5
x=6 y=8