ArrayList.Item プロパティとは? わかりやすく解説

ArrayList.Item プロパティ

指定したインデックスにある要素取得または設定します

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentOutOfRangeException

index が 0 未満です。

または

indexCount上です。

解説解説
使用例使用例

ArrayList作成しいくつかの項目を追加するコード例次に示します。この例では、Item プロパティ (C# の場合インデクサ) によって要素アクセスし、指定されインデックスに対して新しい値を Item プロパティ割り当てることによって要素変更してます。また、Item プロパティ使ってリスト現在のサイズ越えて要素アクセスしたり、要素追加したできないこと示してます。

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class Example

    Public Shared Sub Main

        ' Create an empty ArrayList, and add some elements.
        Dim stringList As New
 ArrayList

        stringList.Add("a")
        stringList.Add("abc")
        stringList.Add("abcdef")
        stringList.Add("abcdefg")

        ' Item is the default property, so the property name is
        ' not required.
        Console.WriteLine("Element {0} is ""{1}""",
 2, stringList(2))

        ' Assigning a value to the property changes the value of
        ' the indexed element.
        stringList(2) = "abcd"
        Console.WriteLine("Element {0} is ""{1}""",
 2, stringList(2))

        ' Accessing an element outside the current element count
        ' causes an exception. The ArrayList index is zero-based,
        ' so the index of the last element is (Count - 1). 
        Console.WriteLine("Number of elements in the list: {0}",
 _
            stringList.Count)
        Try
            Console.WriteLine("Element {0} is ""{1}""",
 _
                stringList.Count, _
                stringList(stringList.Count))
        Catch aoore As ArgumentOutOfRangeException
            Console.WriteLine("stringList({0}) is out of range.",
 _
                stringList.Count)
        End Try

        ' You cannot use the Item property to add new elements.
        Try
            stringList(stringList.Count) = "42"
        Catch aoore As ArgumentOutOfRangeException
            Console.WriteLine("stringList({0}) is out of range.",
 _
                stringList.Count)
        End Try

        Console.WriteLine()
        For i As Integer
 = 0 To stringList.Count - 1
            Console.WriteLine("Element {0} is ""{1}""",
 i, stringList(i))
        Next

        Console.WriteLine()
        For Each o As Object
 In stringList
            Console.WriteLine(o)
        Next

    End Sub

End Class
'
' This code example produces the following output:
'
'Element 2 is "abcdef"
'Element 2 is "abcd"
'Number of elements in the list: 4
'stringList(4) is out of range.
'stringList(4) is out of range.
'
'Element 0 is "a"
'Element 1 is "abc"
'Element 2 is "abcd"
'Element 3 is "abcdefg"
'
'a
'abc
'abcd
'abcdefg
using System;
using System.Collections;

public class Example
{
    public static void Main()
    {
        // Create an empty ArrayList, and add some elements.
        ArrayList stringList = new ArrayList();

        stringList.Add("a");
        stringList.Add("abc");
        stringList.Add("abcdef");
        stringList.Add("abcdefg");

        // The Item property is an indexer, so the property name is
        // not required.
        Console.WriteLine("Element {0} is \"{1}\"", 2, stringList[2]);

        // Assigning a value to the property changes the value of
        // the indexed element.
        stringList[2] = "abcd";
        Console.WriteLine("Element {0} is \"{1}\"", 2, stringList[2]);

        // Accessing an element outside the current element count
        // causes an exception. 
        Console.WriteLine("Number of elements in the list:
 {0}", 
            stringList.Count);
        try
        {
            Console.WriteLine("Element {0} is \"{1}\"", 
                stringList.Count, stringList[stringList.Count]);
        }
        catch(ArgumentOutOfRangeException aoore)
        {
            Console.WriteLine("stringList({0}) is out of range.", 
                stringList.Count);
        }

        // You cannot use the Item property to add new elements.
        try
        {
            stringList[stringList.Count] = "42";
        }
        catch(ArgumentOutOfRangeException aoore)
        {
            Console.WriteLine("stringList({0}) is out of range.", 
                stringList.Count);
        }

        Console.WriteLine();
        for (int i = 0; i < stringList.Count;
 i++)
        {
            Console.WriteLine("Element {0} is \"{1}\"", i, 
                stringList[i]);
        }

        Console.WriteLine();
        foreach (object o in stringList)
        {
            Console.WriteLine(o);
        }
    }
}
/*
 This code example produces the following output:

Element 2 is "abcdef"
Element 2 is "abcd"
Number of elements in the list: 4
stringList(4) is out of range.
stringList(4) is out of range.

Element 0 is "a"
Element 1 is "abc"
Element 2 is "abcd"
Element 3 is "abcdefg"

a
abc
abcd
abcdefg
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

ArrayList.Item プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



ArrayList.Item プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS