質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

2208閲覧

Unexpected symbol ']',expecting';' or '}'・Unexpected symbol '[',expecting'.'

kanade2016

総合スコア60

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2016/11/16 08:42

編集2016/11/16 14:40

Unexpected symbol ']',expecting';' と '}'・Unexpected symbol '[',expecting'.' というエラーが

Stack( List<int> y,int head,int tail){ this.y = int[y.Length+1]; this.head=0; this.tail=y.Length+1; }

のthis.y = int[y.Length+1];の行で出ました。
どこを直せば良いのでしょうか?
全文は

using System; namespace List { class MainClass { public static void Main (string[] args) { Console.WriteLine ("Hello World!"); List<int> a = new List<int> (); List<double> b = new List<double> (); a.Add (10); a.Add (20); a.Add (30); b.Add (40); for (int i = 0; i < a.Length; i++) { Console.WriteLine (a.Get(i)); } } } class List<T> { public T[] a; public int Length; public List() { Length = 0; a = new T[0]; } public void Add(T v){ T[] b = new T[a.Length+1]; for (int i = 0; i < a.Length; i++) { b[i] = a[i]; } b [a.Length] = v; a = b; Length = b.Length; } public T Get(int i){ return a[i]; } public void Delete(T v){ T[] c = new T[a.Length-1]; for (int i = 0; i < a.Length; i++) { c[i] = a[i]; } c [a.Length] = v; a = c; Length = c.Length; } } class Stack { List<int> y; int head; int tail; Stack( List<int> y,int head,int tail){ this.y = int[y.Length+1]; this.head=0; this.tail=y.Length+1; } public static void main (List<int> y,int head,int tail) { Stack stack = new Stack(); } public void Push(int i){ for (int x = 0; x < y.Length; x++){ y.Add(i); } } public void Pop(int i){ for (int x = 0; x < y.Length; x++){ y.Delete (i); } } } class Queue { public Queue() { } public void Push(int i){ List<int> z = new List<int> (); z.Add (i); z.Delete (i); } } }

のように書いていて、Stackを勉強のために実装しています。
どこを直せば良いのか教えてください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

himakuma

2016/11/16 09:03

コードのインデントをそろえてくれませんか?
guest

回答1

0

ベストアンサー

namespaceに対しての } が足りないように見えますが?

} 以前にエラーでませんか?
下記の修正コードで実行までできました

C#

1using System; 2 3namespace List 4{ 5 class MainClass 6 { 7 public static void Main(string[] args) 8 { 9 Console.WriteLine("Hello World!"); 10 List<int> a = new List<int>(); 11 List<double> b = new List<double>(); 12 a.Add(10); 13 a.Add(20); 14 a.Add(30); 15 b.Add(40); 16 17 for (int i = 0; i < a.Length; i++) 18 { 19 Console.WriteLine(a.Get(i)); 20 } 21 } 22 } 23 24 25 26 class List<T> 27 { 28 public T[] a; 29 public int Length; 30 31 public List() 32 { 33 Length = 0; 34 a = new T[0]; 35 } 36 37 38 public void Add(T v) 39 { 40 T[] b = new T[a.Length + 1]; 41 for (int i = 0; i < a.Length; i++) 42 { 43 b[i] = a[i]; 44 } 45 b[a.Length] = v; 46 a = b; 47 Length = b.Length; 48 } 49 50 public T Get(int i) 51 { 52 return a[i]; 53 } 54 55 public void Delete(T v) 56 { 57 T[] c = new T[a.Length - 1]; 58 for (int i = 0; i < a.Length; i++) 59 { 60 c[i] = a[i]; 61 } 62 c[a.Length] = v; 63 a = c; 64 Length = c.Length; 65 } 66 } 67 68 69 class Stack 70 { 71 List<int> y; 72 int head; 73 int tail; 74 75 Stack(List<int> y, int head, int tail) 76 { 77 this.y = new List<int>(); // ←配列的に定義しているのは型が違うから無理 78 this.head = 0; 79 this.tail = y.Length + 1; 80 } 81 82 public static void main(List<int> y, int head, int tail) 83 { 84 Stack stack = new Stack(y, head, tail); // ←コンストラクタを定義しているので引数なしは不可 85 86 } 87 88 public void Push(int i) 89 { 90 for (int x = 0; x < y.Length; x++) 91 { 92 y.Add(i); 93 } 94 } 95 public void Pop(int i) 96 { 97 for (int x = 0; x < y.Length; x++) 98 { 99 y.Delete(i); 100 } 101 } 102 103 } 104 105 class Queue 106 { 107 public Queue() 108 { 109 } 110 public void Push(int i) 111 { 112 List<int> z = new List<int>(); 113 z.Add(i); 114 z.Delete(i); 115 } 116 } 117 118}

投稿2016/11/16 09:01

編集2016/11/17 13:13
himakuma

総合スコア952

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kanade2016

2016/11/16 14:41

記載していないコードがあったので記載しました。上記のが全体の形です。}の数は足りていたようです。別の何かでエラーが出ているみたいです...。
himakuma

2016/11/16 16:42

Stackクラスのyはintの配列ではなくListですね。後存在するクラス名をnamespaceには宣言できないような。。
himakuma

2016/11/16 16:44

Listを自前で組もうとしてます?
kanade2016

2016/11/17 12:56

Listは自前では組もうとしていません。 List<int> yとかいてあったところをList yに書き換えたのですがそれでも同じエラーが出ます。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問