為什么這段代碼不起作用?

[英]Why this code is not working?


Hi guys I wrote this code and i have two errors.

嗨,大家好,我寫了這個代碼,我有兩個錯誤。

  1. Invalid rank specifier: expected ',' or ']'
  2. 無效的排名說明符:預期','或']'

  3. Cannot apply indexing with [] to an expression of type 'int'
  4. 無法將帶有[]的索引應用於類型為'int'的表達式

Can you help please?

你能幫幫忙嗎?

    static void Main(string[] args)
    {
        ArrayList numbers = new ArrayList();

        foreach (int number in new int[12] {10,9,8,7,6,5,4,3,2,1}) //error No.1
        {
            numbers.Add(number);
        }

        numbers.Insert(numbers.Count - 1, 75);
        numbers.Remove(7);
        numbers.RemoveAt(6);

        for(int i=0; i<numbers.Count; i++)
        {
            int number = (int) number[i]; // error No.2
            Console.WriteLine(number);
        }
    }

5 个解决方案

#1


3  

using System;
using System.Collections;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList numbers = new ArrayList();
            foreach (int number in new int[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 })
            {
                numbers.Add(number);
            }
            numbers.Insert(numbers.Count - 1, 75);
            numbers.Remove(7);
            numbers.RemoveAt(6);
            for (int i = 0; i < numbers.Count; i++)
            {
                int number = (int)numbers[i];
                Console.WriteLine(number);
            }
        }
    }
}

#2


6  

1 - You don't have to specify the length of the array just say new int[]

1 - 您不必指定數組的長度只需說新的int []

2 - number is just an integer, I think you're trying to access numbers[i]

2 - 數字只是一個整數,我想你正試圖訪問數字[i]

#3


4  

For 1:

foreach (int number in new int[] {10,9,8,7,6,5,4,3,2,1})

For 2:

int number = (int)numbers[i];

You are using number where you should have numbers (plural).

您正在使用數字,您應該有數字(復數)。

#4


0  

Why not the following for #1?

為什么不是#1的以下內容?

    for (int x = 10; x > 0; --x)
    {
        numbers.Add(number);
    }

Despite declaring this as an int[12] (as the apparent intent?), it seems like we're only using the values from 10 to 1, inclusive. Why use a foreach in this scenario, when a for is much more clear in its intent?

盡管聲明這是一個int [12](作為明顯的意圖?),似乎我們只使用10到1的值,包括10和1。為什么在這種情況下使用foreach,當for的意圖更明確?

#5


0  

You should be initializing the array as

您應該將數組初始化為

new int[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

the compiler will set the size for you. But you're doing it the hard way. Try this:

編譯器將為您設置大小。但是你正在艱難地做這件事。試試這個:

for (int i = 10; i > 0; i-- )
{
    numbers.Add(i);
}

If you are using .Net 3.5, you can also use System.Linq.Enumerable to create a range:

如果您使用的是.Net 3.5,還可以使用System.Linq.Enumerable創建范圍:

IEnumerable<int> numbers = Enumerable.Range(1, 10).Reverse();

This would take the place of the ArrayList, which is pretty pointless in 3.5. Since you're just starting, the ArrayList will probably be easier to grasp at first, but keep things like Generics and IEnumerables in mind, they are very important.

這將取代ArrayList,這在3.5中毫無意義。由於剛剛開始,ArrayList最初可能更容易掌握,但是記住Generics和IEnumerables之類的東西,它們非常重要。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/10/23/72549557ec70faeba348045873ce067d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com