Matrice et Arraylist
Fermé
euphina
Messages postés
6
Date d'inscription
mardi 8 mars 2016
Statut
Membre
Dernière intervention
1 avril 2017
-
Modifié le 1 avril 2017 à 13:09
KX Messages postés 16668 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 17 mars 2023 - 1 avril 2017 à 13:23
KX Messages postés 16668 Date d'inscription samedi 31 mai 2008 Statut Modérateur Dernière intervention 17 mars 2023 - 1 avril 2017 à 13:23
1 réponse
KX
Messages postés
16668
Date d'inscription
samedi 31 mai 2008
Statut
Modérateur
Dernière intervention
17 mars 2023
3 005
1 avril 2017 à 13:23
1 avril 2017 à 13:23
Bonjour,
Voici un exemple :
Voici un exemple :
// [[a, b, c, d], [e, f, g, h], [i, j, k, l]]
char[][] tab = convert("abcdefghijklmnopqrstuvwxyz", 3, 4);
public static char[][] convert(String str, int n, int m) {
if (str.length() < n * m) {
throw new IllegalArgumentException("Un String avec " + str.length()
+ " caractères est trop petit pour remplir une matrice de "
+ n + " x " + m + " caractères");
}
char[][] result = new char[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
result[i][j] = str.charAt(i * m + j);
return result;
}