配列の初期化方法にはどのようなものがありますか?

DDD
リリース: 2023-09-05 09:44:44
オリジナル
2366 人が閲覧しました

配列の初期化メソッドには次のものが含まれます: 1. 宣言と代入; 2. コンストラクターの使用; 3. ループの使用; 4. 配列の fill() メソッドの使用; 5. 配列の map() メソッドの使用; 6. を使用新しいキーワードと配列のコンストラクター; 7. リスト内包表記の使用; 8. Array.prototype.fill() の使用。

配列の初期化方法にはどのようなものがありますか?

#プログラミングにおいて、配列は同じ型のデータ要素を格納するために使用されるデータ構造です。配列の初期化はプログラミング言語によって異なりますが、一般にいくつかの一般的な方法があります。いくつかのプログラミング言語における配列の初期化方法を以下に示します:

1. 宣言と代入: 多くのプログラミング言語では、配列を直接宣言して初期化できます。たとえば、JavaScript の場合:

let arr = [1, 2, 3, 4, 5];
ログイン後にコピー

Python の場合:

arr = [1, 2, 3, 4, 5]
ログイン後にコピー

2. コンストラクターを使用する: 一部のプログラミング言語では、配列の初期化専用のコンストラクターが提供されています。たとえば、Java の場合:

int[] arr = new int[]{1, 2, 3, 4, 5};
ログイン後にコピー

C の場合:

int arr[] = {1, 2, 3, 4, 5};
ログイン後にコピー

3. ループの使用: ループを使用して、配列の各要素を初期化できます。たとえば、JavaScript の場合:

let arr = new Array(5);  
for (let i = 0; i < arr.length; i++) {  
    arr[i] = i + 1;  
}
ログイン後にコピー

Python の場合:

arr = [0] * 5  
for i in range(5):  
    arr[i] = i + 1
ログイン後にコピー

4. 配列の fill() メソッドを使用します。一部のプログラミング言語には、配列を埋めるための組み込みメソッドが用意されています。たとえば、JavaScript の場合:

let arr = new Array(5).fill(1);
ログイン後にコピー

Python の場合:

arr = [0] * 5
ログイン後にコピー

5. 配列の map() メソッドを使用します: 配列の map() メソッドを使用して配列を初期化できます。 。たとえば、JavaScript の場合:

let arr = Array.from({length: 5}, (_, i) => i + 1);
ログイン後にコピー

Python の場合:

arr = list(range(1, 6))
ログイン後にコピー

6. 新しいキーワードと配列のコンストラクターを使用します。一部のプログラミング言語では、新しいキーワードとコンストラクターを使用して、そして配列を初期化します。たとえば、C の場合:

int arr[5] = {1, 2, 3, 4, 5};
ログイン後にコピー

7. リスト内包表記の使用 (リスト内包表記): リスト内包表記をサポートする一部のプログラミング言語では、この機能を使用して配列を初期化できます。たとえば、Python の場合:

arr = [i for i in range(1, 6)]
ログイン後にコピー

8. Array.prototype.fill() の使用: 一部のプログラミング言語では、Array.prototype.fill() メソッドを使用して配列を埋めることができます。たとえば、JavaScript の場合:

let arr = new Array(5).fill(1); // Fill existing array with a value. The length of the array stays the same. 5 items. Each with a value of &#39;1&#39;. 1-based index. Starting at index 0. The fill() method is not chainable. It doesn&#39;t return the original array. It returns a new array. The original array is not modified. It returns a new array of the same length with all items set to the specified value. If you specify only one argument, it sets all items to that value. If you specify more than one argument, it uses a provided generator function to produce values for the new array. If you specify a generator function, it should take two arguments: the current index and the current value. It should return the new value for the array element. It is called for each index from 0 to length - 1. It can also be used with a generator expression that iterates over a sequence of values and returns an array of those values. The fill() method can be used to create new arrays with a specified length and all items set to the same value. It can also be used to fill an existing array with a new value. It returns a new array of the same length with all items set to the specified value. If you specify only one argument, it sets all items to that value. If you specify more than one argument, it uses a provided generator function to produce values for the new array. If you specify a generator function, it should take two arguments: the current index and the current value. It should return the new value for the array element. It is called for each index from 0 to length - 1. It can also be used with a generator expression that iterates over a sequence of values and returns an array
ログイン後にコピー

以上が配列の初期化方法にはどのようなものがありますか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!