C#のArray.AsReadOnly(T [])メソッド
C#のArray.AsReadOnly(T [])メソッドは、指定された配列の読み取り専用ラッパーを返します。これは、読み取り専用のReadOnlyCollection
構文
public static System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly<T> (T[] array);
ここで、Tは配列の要素の型であり、配列T[]は1次元のゼロベースの配列です。
Array.AsReadOnly(T [])メソッドを実装する例を見てみましょう-
例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
String[] arr = { "John", "Tom", "Katie", "Brad" };
// read-only IList wrapper
IList<String> list = Array.AsReadOnly( arr );
// Display the values of the read-only IList.
Console.WriteLine( "Initial read-only IList..." );
display( list );
// Let us now change the read-only wrapper
try {
list[0] = "Kevin";
list[1] = "Bradley";
}
catch ( NotSupportedException e ) {
Console.WriteLine(e.GetType());
Console.WriteLine(e.Message );
Console.WriteLine();
}
Console.WriteLine( "After changing two elements, the IList remains the same since it is read-only..." );
display( list );
}
public static void display( IList<String> list ) {
for ( int i = 0; i < list.Count; i++ ) {
Console.WriteLine(list[i] );
}
Console.WriteLine();
}
} 出力
これにより、次の出力が生成されます-
Initial read-only IList... John Tom Katie Brad System.NotSupportedException Collection is read-only. After changing two elements, tthe IList remains the same since it is read-only... John Tom Katie Brad
-
JavaScript配列from()メソッド
JavaScriptのfrom()メソッドは、lengthプロパティを持つ任意のオブジェクトまたは反復可能なオブジェクトからArrayオブジェクトを返すために使用されます。 構文は次のとおりです- Array.from(obj, mapFunction, val) 上記では、パラメータobjは配列に変換するオブジェクト、mapFunctionは呼び出すマップ関数、valはmapFunctionを実行するときにこれとして使用する値です。 JavaScriptでfrom()メソッドを実装しましょう- 例 <!DOCTYPE html> <html> <body
-
JavaScript Array.join()メソッド
JavaScriptのArray.join()メソッドを使用して、配列を結合し、文字列として返します。 構文は次のとおりです- array.join(separator) 上記で、パラメータとして使用するセパレータを設定します。 ここで、JavaScriptマイナスでArray.join()メソッドを実装しましょう; 例 <!DOCTYPE html> <html> <body> <h2>Demo Heading</h2> <p>Is this is an ar