• Aucun résultat trouvé

UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD3 - Tableau

N/A
N/A
Protected

Academic year: 2022

Partager "UE 2I002 (ex LI230) : éléments de programmation par objets avec Java TD3 - Tableau"

Copied!
27
0
0

Texte intégral

(1)

UE 2I002 (ex LI230) : éléments de programmation par objets avec Java

TD3 - Tableau

Juliana Silva Bernardes

[email protected]

http://www.lcqb.upmc.fr/julianab/teaching/JAVA/

(2)

‣Tableau d'eléments de type simple

‣Tableau de tableaux (deux dimensions)

‣Tableau d’objets à une dimension

Sumary

(3)

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

(4)

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)

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.

(6)

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)

7

Tableau de tableaux (deux dimensions)

(8)

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)

9

Tableau de tableaux (deux dimensions)

‣ Un tableau à plusieurs dimensions peut être initialisé : int t21[][] = {{1, 2, 3},

{4, 5, 6}};

(10)

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)

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

}

(12)

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)

13

Tableau de tableaux (deux dimensions)

‣ Exemple : Declarer et inicialiser un tableau pour representer la figure

* * * *

* * *

* *

*

(14)

Tableau de tableaux (deux dimensions)

‣ Exemple : Declarer et inicialisé un tableau pour representer la figure char tFigure[][] = new char[4][];

tFigure

0 1

* * * *

* * *

* *

*

(15)

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

* * * *

* * *

* *

*

(16)

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)

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

* * * *

(18)

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)

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

* * * *

* * *

* *

*

(20)

Tableau d’objets à une dimension

(21)

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

(22)

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)

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

(24)

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)

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

(26)

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)

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

Références

Documents relatifs

– et d’e↵ectuer les instructions n´ecessaires pour la cr´eation d’un objet de cette classe Les constructeurs sont appel´es quand on cr´ee (mot-cl´e new) un objet de cette

On ne peut pas utiliser l’opérateur « point » (.) sur les variables de la classe Chien ailleurs que dans la classe Chien

UE 2I002 (ex LI230) : éléments de programmation par objets avec Java!. TD4 - Composition,

‣ boolean, byte, short, char, int, long, float et double, sont associées des classes Boolean, Byte, Short, Character, Integer, Long, Float et Double. ‣ Ces classes ont des

6OF EFT QSPQSJÏUÏT JOEVJUFT QBS MF QPMZNPSQIJTNF FTU RVF MJOUFSQSÏUFVS +BWB FTU DBQBCMF EF USPVWFS MF USBJUFNFOU Ë FČFDUVFS MPST EF MBQQFM EVOF NÏUIPEF TVS VO PCKFU &#34;JOTJ

-F DPODFQU EF DMBTTF BCTUSBJUF TF TJUVF FOUSF DFMVJ EF DMBTTF FU DFMVJ EJOUFSGBDF $FTU VOF DMBTTF RVPO OF QFVU QBT EJSFDUFNFOU JOTUBODJFS DBS DFSUBJOFT EF TFT NÏUIPEFT OF TPOU

‣ On peut affecter à un champ ou une variable d'un type une expression de type moins élevé dans la hiérarchie des types. int a

• La sous-couche p peut contenir au maximum 6 électrons Exemple : Atome de soufre S (Z= 16) 16 électrons à répartir.. Thème 1 : CONSTITUTION ET TRANSFORMATIONS DE LA MATIÈRE