Skip to content

Les Tableaux

lcs01 edited this page Jan 25, 2019 · 2 revisions

ArraysList Array tab

TABLEAU SIMPLE

import java.util.Arraylist Exemple effectué en cours:

On a commencé comme cela

	public static void main(String[] args) {
	int[] tab; //declaration
	tab = new int[5]; //instanciation
	tab[0] = 2; 

// [0] est l'index(position) et 2 est la valeur attribuée à cette case

	System.out.println(tab[0] +"ceci sera supprimé"); 
           //[I@7852e922 adresse mémoire
           //I pour int @ qui pointe vers la mémoire 7852e922

	for(int i = 0; i<tab.length; i++) {
		tab[i] = (int)(Math.random()*1000);
		System.out.println(tab[i]); // le ; dit "fin d'instruction 
	} 

ensuite nous avons repris et procédés comme ceci en incluant des fonctions

public static void affichage(int tab[]) { //1er version for (int i = 0; i < tab.length; i++) { System.out.println(tab[i]); } }

public static int[] randomMath() {  //1er version
	int tab[] = new int[5];
	for (int i = 0; i < tab.length; i++) {
		tab[i] = (int)(Math.random()*1000);
	}
	return tab;
} 


public static void main(String[] args) {
		int[] tabTest= randomMath(5); 1er version  
                   //fonction qui a un retour = on stock dans une variable

		affichage(tabTest); lié au 1er essai  
                  //fonction sans retour + en argument tabTest
           }

puis pour finir comme cela

/**

  • fonction qui me permet d'afficher les crochets et les virgules

  • @param tab */ public static void affichage(int tab[]) { //2eme version System.out.print("["); for (int i = 0; i < tab.length; i++) { if(i != 0) { System.out.print(", "); } System.out.print(tab[i]); } System.out.print("]"); System.out.println(); }

    /**

    • fonction qui me permet de parametrer une taille de tableau grace à l'argument *int taille

    • et de remplir avec des entiers aleatoires

    • @param taille

    • @return */ public static int[] randomMath(int taille) { //2eme version int tab[] = new int[taille]; for (int i = 0; i < tab.length; i++) { tab[i] = (int)(Math.random()*1000); } return tab; }

         public static void main(String[] args) {
      
                    affichage(randomMath(5));
       // je passe en argument (int taille) le nombre de 5
      

    }

}

TABLEAU A DEUX DIMENSIONS

Exercice réalisé en cours:

import java.util.Arrays;

public class ExoMatrice { /** * fonction qui a pour parametre un entier T qui genere * une matrice aleatoire (compris entre 0 et 100) *
* une fonction qui a pour parametre une matrice et qui affiche cette matrice * @param args */

public static int[][] matriceAlea(int ligne, int colonne, int aleaMax) {
	//j'ai rentré en argument ma ligne, ma colonne que je peux passer en para
	//j'ai crée un argument pour modifier ma fonction "nbAlea" qui se nomme aleaMax

	int[][] tab = new int[ligne][colonne];
	for (int i = 0; i < ligne; i++) {
		for (int j = 0; j < colonne; j++) {

	//tab[i][j] += nbAlea(aleaMax); 1er façon procédés

	tab[i][j] += (int)(Math.random()*aleaMax);
	//ma deuxieme façon qui me permet de supprimer la fonction supperflu qui est "nombre alea"

	/*System.out.println("i = "+ i + " j = "+ j);
	System.out.println("table de i"  + Arrays.toString(tab[i]));
	System.out.println("element i j " + tab[i][j]); */
	// les syso sont la pour detailler tour par tour leurs valeurs
		}

	}

	return tab;
}

public static void affiche(int[][] tab) {
	for(int i=0; i <tab.length; i++) {
		int[] ligne = tab[i];
		//je décompose mon code ou j'aurai pû faire j < tab[i] dans ma boucle
		//je dis que j doit parcourir la longueur de tab [i]

		for(int j=0; j<ligne.length; j++) {
			System.out.print(tab[i][j]+ " ");
		}
		System.out.println();
	}

}
/**
 * Fonction qui me retourne le nombre aleatoire entre 0 et max
 * 
 * @param max
 * @return
 *
public static int nbAlea(int max) {
	return (int)(Math.random()*max);
	// on peut directement return sans avoir besoin de crée une nouvelle variable
	// et le renvoyé dans nbAlea en argument "Max"
 * 
 * Ma fonction devient obsoléte vu que je déclare Math random dans ma fonction
 * matriceAlea
}
 */
public static void main(String[] args) {
	int[][] tab = matriceAlea(3, 5, 10);
	//le 3 correspond à l'argument ligne
	// le 5 correspond à l'argument colonne
	// le 10 correspond à l'argument aleaMax
	affiche(tab);

}

}

Clone this wiki locally