[ java ] méthode surcharge multidimensionnel
domxaline
-
domxaline -
domxaline -
Bonjour,
j'ai essayé ce prg pour multidimensionnelle méthode surcharge, en le compilant ce dernier me donne une erreur suivante aidez moi svp:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method parcourrirTab(String[][]) in the type Sdz1 is not applicable for the arguments (String[])
at Sdz1.main(Sdz1.java:6)
j'ai essayé ce prg pour multidimensionnelle méthode surcharge, en le compilant ce dernier me donne une erreur suivante aidez moi svp:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method parcourrirTab(String[][]) in the type Sdz1 is not applicable for the arguments (String[])
at Sdz1.main(Sdz1.java:6)
public class Sdz1
{
public static void main(String[] args)
{
String[]tab={"toto","tata","titi","tete"};
parcourrirTab (tab);
System.out.println (tab);
}
static void parcourrirTab (String[][]tab)
{
for(String tab2[]:tab)
{
for(String str:tab2)
System.out.println(str);
}
}
}
A voir également:
- [ java ] méthode surcharge multidimensionnel
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages
- Waptrick java voiture - Télécharger - Jeux vidéo
5 réponses
Pour surcharger il faut que tu ai 1 méthode différente pour chaque cas :
public class Sdz1 { public static void main(String[] args) { String[] tab = { "toto", "tata", "titi", "tete" }; String[][] tab2 = {{ "toto", "tata"},{ "titi", "tete" }}; String s = "test"; parcourrirTab(tab); parcourrirTab(tab2); parcourrirTab(s); } static void parcourrirTab(String[] tab) { System.out.println("la"); } static void parcourrirTab(String[][] tab) { System.out.println("ici"); } static void parcourrirTab(String tab) { System.out.println("coucou"); } }
j'ai besoin encore une aide
je veux ajouter un int, comme paramètres dans le méthode parcourrirTab
mails il dit erreur
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method parcourrirTab(String[][], int) in the type Sdz1 is not applicable for the arguments (String[][])
at Sdz1.main(Sdz1.java:6)
merci d'avance
je veux ajouter un int, comme paramètres dans le méthode parcourrirTab
mails il dit erreur
public class Sdz1
{
public static void main(String[] args)
{
String[][]tab2={{"toto","tata"},{"titi","tete"}};
parcourrirTab (tab2);
}
static void parcourrirTab (String[][]tab,int i)
{
for(String tab2[]:tab)
{
for(String str:tab2)
System.out.print(str+",");
}
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method parcourrirTab(String[][], int) in the type Sdz1 is not applicable for the arguments (String[][])
at Sdz1.main(Sdz1.java:6)
merci d'avance
tu a ajouté un entier dans les parametres de la fonction:
static void parcourrirTab (String[][]tab,int i)
donc la fonction s'attend à recevoir un tableau plus un entier donc lors de l'appel à la fonction (lors de l'utilisation de la fonction) :
parcourrirTab (tab2);
il faut lui donner cet entier dont il a maintenant besoin
static void parcourrirTab (String[][]tab,int i)
donc la fonction s'attend à recevoir un tableau plus un entier donc lors de l'appel à la fonction (lors de l'utilisation de la fonction) :
parcourrirTab (tab2);
il faut lui donner cet entier dont il a maintenant besoin
Remarque : il n'est pas nécessaire de faire de surcharge pour ce genre de code.
Il est tout à fait possible de faire une seule méthode pour afficher les objets (String ou autre), qu'ils soient passés seuls en paramètres, dans un tableau, ou une Collection (listes, arbres, etc.). Cette méthode reste valable y compris pour des types plus compliqués (tableaux doubles, triples, Collection imbriquées, etc.)
Il est tout à fait possible de faire une seule méthode pour afficher les objets (String ou autre), qu'ils soient passés seuls en paramètres, dans un tableau, ou une Collection (listes, arbres, etc.). Cette méthode reste valable y compris pour des types plus compliqués (tableaux doubles, triples, Collection imbriquées, etc.)
public class Test { public static void afficher(Object val) { if (val.getClass().isArray()) { for (Object obj : (Object[]) val) afficher(obj); } else if (val instanceof Iterable) { for (Object obj : (Iterable<?>) val) afficher(obj); } else { System.out.println(val); } } public static void main(String[] args) { afficher(new String("a")); afficher(new String[] {"b","c","d"}); afficher(new String[][] {{"e","f","g"},{"h","i"}}); afficher(new String[][][] {{{"j","k"},{"l","m"}},{{"n","o"},{"p","q"}}}); // Collections afficher(new LinkedList<String>(Arrays.asList("r","s","t","u"))); afficher(new TreeSet<String>(Arrays.asList("v","w","x","y","z"))); }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question