cs_domxaline
Messages postés327Date d'inscriptionjeudi 21 août 2008StatutMembreDernière intervention10 mai 2012
-
13 avril 2012 à 22:03
cs_Julien39
Messages postés6414Date d'inscriptionmardi 8 mars 2005StatutModérateurDernière intervention29 juillet 2020
-
19 avril 2012 à 08:53
Bonjour,
j'ai un pb initialiser mon arrays;je veux avoir résultat suivant:
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
mais j'ai erreur en compilant mon prg
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at TestArrays2.main(TestArrays2.java:17)
voilà mon prg
public class TestArrays2
{
public static void main(String[]args)
{
int i;
int j;
int table [][]=new int [5][5];
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
if(i==j)
table[i][j]=1;
else
table[i][j]=0;
}
System.out.print(i);
System.out.print(j);
}
}
veuillez m'aider svp
résulat que je obtiens
0515253545
cs_GodConan
Messages postés2113Date d'inscriptionsamedi 8 novembre 2003StatutContributeurDernière intervention 6 octobre 201212 14 avril 2012 à 09:44
Salut,
Si ta matrice est constante, init la en constante :
int[][] matrice = {{,,,},{...}...};
sinon utilise la taille de ton tableau dans la boucle et non une constante ("5")
int[][] tab = new int[5][5]; // par default tout est à 0
//init
for(int i=0;i<tab.length;i++) // on utilise la sysmétrie de la table
tab[i][i]=1;
// affichage
System.out.println("je veux : ;o)");
for(int i=0;i<tab.length;i++) { // on utilise la sysmétrie de la table
for(int j=0;j<tab[i].length;j++)
System.out.print(tab[i][j] + " ");
System.out.print("\n"); }