前提・実現したいこと
ここに質問の内容を詳しく書いてください。
JAVAでスタックにInt[2]型({1,2},{3,4},{5,6})をプッシュしたいのですが
書き方がわかりません。
どなたかご教示下さい。
push(5,6)
push(3,4) (3,4)
push(1,2) (1,2) (1,2)
発生している問題・エラーメッセージ
エラーメッセージ 型 Stack<Integer> のメソッド push(Integer) は引数 (List<Integer>) に適用できません
該当のソースコード
ソースコード import java.util.Scanner; import java.util.Stack; public class Main { static final int[] dx = { -1, 0, 1, 0 }; static final int[] dy = { 0, 1, 0, -1 }; static int N; static char[][] map; static String string; public static void main(String[] args) { // 自分の得意な言語で // Let's チャレンジ!! Scanner sc = new Scanner(System.in); N = sc.nextInt(); map = new char[N][]; for(int i=0; i<N; i++){ map[i]=sc.next().toCharArray(); } int M = sc.nextInt(); for(int i=0; i<M; i++){ string = sc.next(); int x=0; int y=0; Stack<int[]> visited = new Stack<>(); Boolean writable = false; for(int j=0; j<N; j++){ for(int k=0; k<N; k++){ if(string.charAt(0)==map[j][k]){ y=j; x=k; int[] next = {y,x}; visited.push(next); int count=1; if(dfs(visited,y,x,count)){ System.out.println("yes"); writable=true; break; } } } if(writable){ break; } } if(!writable){ System.out.println("no"); } } } public static boolean dfs(Stack<int[]> visited, int sh, int sw, int count){ if(count==string.length()){ return true; }else{ for (int i = 0; i < 4; i++) { int nw = sw + dx[i]; int nh = sh + dy[i]; int[] next = {nh, nw}; if (nw < 0 || nw >= N || nh < 0 || nh >= N) { continue; }else if(map[nh][nw]==string.charAt(count) && !visited.contains(next)){ visited.push(next); count+=1; if(dfs(visited, nh, nw, count)){ return true; }else{ count-=1; visited.pop(); } } } return false; } } }
試したこと
ここに問題に対して試したことを記載してください。
ネットでジェネリクスなどいろいろ調べましたがわかりませんでした。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
eclipse でコンパイルが通りません。
Integer 型の引数をもつメソッドに配列、またはリストを渡してもループして処理されることはありません。
エラーメッセージを理解することは必須です
Stackを自作して配列をpushできるようにするという事でしょうか?
コード全体を載せてください。
import java.util.Scanner; import java.util.Stack; public class Main { static final int[] dx = { -1, 0, 1, 0 }; static final int[] dy = { 0, 1, 0, -1 }; static int N; static char[][] map; static String string; public static void main(String[] args) { // 自分の得意な言語で // Let's チャレンジ!! Scanner sc = new Scanner(System.in); N = sc.nextInt(); map = new char[N][]; for(int i=0; i<N; i++){ map[i]=sc.next().toCharArray(); } int M = sc.nextInt(); for(int i=0; i<M; i++){ string = sc.next(); int x=0; int y=0; Stack<int[]> visited = new Stack<>(); Boolean writable = false; for(int j=0; j<N; j++){ for(int k=0; k<N; k++){ if(string.charAt(0)==map[j][k]){ y=j; x=k; int[] next = {y,x}; visited.push(next); int count=1; if(dfs(visited,y,x,count)){ System.out.println("yes"); writable=true; break; } } } if(writable){ break; } } if(!writable){ System.out.println("no"); } } } public static boolean dfs(Stack<int[]> visited, int sh, int sw, int count){ if(count==string.length()){ return true; }else{ for (int i = 0; i < 4; i++) { int nw = sw + dx[i]; int nh = sh + dy[i]; int[] next = {nh, nw}; if (nw < 0 || nw >= N || nh < 0 || nh >= N) { continue; }else if(map[nh][nw]==string.charAt(count) && !visited.contains(next)){ visited.push(next); count+=1; if(dfs(visited, nh, nw, count)){ return true; }else{ count-=1; visited.pop(); } } } return false; } } }
回答2件
あなたの回答
tips
プレビュー