プログラミングにおいて、リストと配列はデータを整理して保存するために使用されるデータ構造です。どちらにも独自の機能と目的があります。 リスト は 動的 そして フレキシブル 実行時に簡単にサイズを変更できます。 配列 は 静的 とともに 固定サイズ 。この違いは、メモリの使用量とパフォーマンスに影響します。
目次
リストとは何ですか?
リストはプログラミングにおける多用途のデータ構造であり、柔軟に処理できる要素のコレクションを保持するように設計されています。 さまざまなデータ型 。配列とは異なり、リストは 動的 つまり、プログラムの実行中にサイズが変更される可能性があります。この適応性により、リストは要素の追加または削除を伴うタスクに特に役立ちます。リストは、開発者がデータを管理および整理するための便利なインターフェイスを提供し、次のような効率的な操作を可能にします。 追加する 、 挿入する 、 または 削除する 要素。サイズを動的に調整できる機能により、リストはプログラム内のさまざまな量のデータを処理するための強力なツールになります。
配列とは何ですか?
配列はプログラミングにおける基本的なデータ構造であり、これを使用すると、要素のコレクションを保存できます。 同じデータ型 メモリの連続ブロック内。配列内の各要素は、その位置を表すインデックスによって識別されます。配列の主な特徴は、これらのインデックスを使用して要素に迅速かつ直接アクセスできることです。データを整理して管理するための体系的な方法を提供し、効率的にデータを整理および管理できます。 取得する 、 修正する 、 そして 操作する 配列に格納される情報。配列は、順序付けされたデータ セットの処理が簡単で効果的であるため、さまざまなプログラミング言語で広く使用されています。
リストと配列の違い:
| 側面 | 配列 | リスト |
|---|---|---|
| サイズ | 配列のサイズは作成時に固定サイズに設定されます。 | リストは動的であり、実行時にサイズが変更される可能性があります。 |
| データ型 | 配列内のすべての要素は同じデータ型である必要があります。 | リストには、さまざまなデータ型の要素を含めることができます。 |
| メモリの割り当て | 配列全体のメモリは、初期化中に一度に割り当てられます。 | リストは要素が追加または削除されると動的にメモリを割り当てます。 |
| アクセス時間 | 配列は、インデックス付けを通じて要素への定時アクセスを提供します。 | リストのアクセス時間は、動的なサイズ変更により若干変動する場合があります。 |
| 柔軟性 | 配列はサイズを簡単に変更できないため、柔軟性が低くなります。 | リストはより柔軟であり、要素を簡単に追加または削除できます。 |
| メモリ効率 Javaは文字列内の文字を置換します | サイズが必要以上に大きい場合、メモリの浪費が発生する可能性があります。 | 動的割り当てによりメモリ効率が向上します。 |
| 一般的な実装 | C/C++ などの言語で一般的です。 | Python や Java などの言語で一般的です。 |
リストの実装:
Python で提供されているコード例では、リストは整数 (10、20、30) を格納するように初期化されています。要素は追加され、インデックスによってアクセスされ、変更され、削除されます。 Pythonでは追加にはappendメソッド、削除にはremoveメソッドを使用します。この例では、これらのプログラミング言語でリストを作成、変更、管理する基本的な操作を示します。
C++
#include> #include> int> main() {> >// Creating an empty vector> >std::vector<>int>>myArray;>>' >myArray.push_back(10);> >myArray.push_back(20);> >myArray.push_back(30);> >// Displaying the elements in the vector> >std::cout <<>'Elements in the vector: '>;> >for> (>int> num : myArray) {> >std::cout << num <<>' '>;> >}> >std::cout << std::endl;> >// Accessing elements by index> >int> firstElement = myArray[0];> >int> secondElement = myArray[1];> >// Modifying an element> >myArray[1] = 25;> >// Removing an element by value> >for> (>auto> it = myArray.begin(); it != myArray.end(); ++it) {> >if> (*it == 30) {> >myArray.erase(it);> >break>;> >}> >}> >// Displaying the updated vector> >std::cout <<>'Updated vector: '>;> >for> (>int> num : myArray) {> >std::cout << num <<>' '>;> >}> >std::cout << std::endl;> >return> 0;> }> // This code is contributed by shivamgupta0987654321> |
>
>
ジャワ
import> java.util.ArrayList;> import> java.util.Iterator;> public> class> Main {> >public> static> void> main(String[] args)> >{> >// Creating an empty ArrayList> >ArrayList myArray =>new> ArrayList();> >// Adding elements to the ArrayList> >myArray.add(>10>);> >myArray.add(>20>);> >myArray.add(>30>);> >// Displaying the elements in the ArrayList> >System.out.print(>'Elements in the ArrayList: '>);> >for> (>int> num : myArray) {> >System.out.print(num +>' '>);> >}> >System.out.println();> >// Accessing elements by index> >int> firstElement = myArray.get(>0>);> >int> secondElement = myArray.get(>1>);> >// Modifying an element> >myArray.set(>1>,>25>);> >// Removing an element by value> >Iterator iterator = myArray.iterator();> >while> (iterator.hasNext()) {> >int> element = iterator.next();> >if> (element ==>30>) {> >iterator.remove();> >break>;> >}> >}> >// Displaying the updated ArrayList> >System.out.print(>'Updated ArrayList: '>);> >for> (>int> num : myArray) {> >System.out.print(num +>' '>);> >}> >System.out.println();> >}> }> |
>
>
Python3
# Creating an empty list> my_list>=> []> # Adding elements to the list> my_list.append(>10>)> my_list.append(>20>)> my_list.append(>30>)> # Displaying the elements in the list> print>('Elements>in> the>list>:', my_list)> # Accessing elements by index> first_element>=> my_list[>0>]> second_element>=> my_list[>1>]> # Modifying an element> my_list[>1>]>=> 25> # Removing an element> my_list.remove(>30>)> # Displaying the updated list> print>('Updated>list>:', my_list)> |
>
>
C#
using> System;> using> System.Collections.Generic;> class> Program> {> >static> void> Main()> >{> >// Creating an empty list> >List<>int>>myArray =>>int>>();>> >myArray.Add(10);> >myArray.Add(20);> >myArray.Add(30);> >// Displaying the elements in the list> >Console.Write(>'Elements in the list: '>);> >foreach> (>int> num>in> myArray)> >{> >Console.Write(num +>' '>);> >}> >Console.WriteLine();> >// Accessing elements by index> >int> firstElement = myArray[0];> >int> secondElement = myArray[1];> >// Modifying an element> >myArray[1] = 25;> >// Removing an element by value> >for> (>int> i = 0; i { if (myArray[i] == 30) { myArray.RemoveAt(i); break; } } // Displaying the updated list Console.Write('Updated list: '); foreach (int num in myArray) { Console.Write(num + ' '); } Console.WriteLine(); } }> |
>
>
JavaScript
// Creating an empty array> let myArray = [];> // Adding elements to the array> myArray.push(10);> myArray.push(20);> myArray.push(30);> // Displaying the elements in the array> console.log(>'Elements in the array:'>, myArray);> // Accessing elements by index> let firstElement = myArray[0];> let secondElement = myArray[1];> // Modifying an element> myArray[1] = 25;> // Removing an element (in this case, removing by value)> let indexToRemove = myArray.indexOf(30);> if> (indexToRemove !== -1) {> >myArray.splice(indexToRemove, 1);> }> // Displaying the updated array> console.log(>'Updated array:'>, myArray);> |
>
>出力
Elements in the vector: 10 20 30 Updated vector: 10 25>
配列の実装:
C++、C、Python、Java、および JavaScript では、コードは要素 (10、20、30) を含む配列を作成し、インデックスによって要素にアクセスして変更し、更新された配列を表示します。構文と具体的なメソッドは言語によって異なりますが、基本的な配列操作は一貫しており、配列の操作方法と反復方法を示しています。
C++
#include> using> namespace> std;> int> main() {> >// Creating an array> >int> myArray[3] = {10, 20, 30};> >// Accessing elements by index> >int> firstElement = myArray[0];> >int> secondElement = myArray[1];> >// Modifying an element> >myArray[1] = 25;> >// Displaying the elements in the array> >for> (>int> i = 0; i <3; ++i) {> >cout << myArray[i] << ' ';> >}> >return> 0;> }> |
>
>
C
#include> int> main() {> >// Creating an array> >int> myArray[3] = {10, 20, 30};> >// Accessing elements by index> >int> firstElement = myArray[0];> >int> secondElement = myArray[1];> >// Modifying an element> >myArray[1] = 25;> >// Displaying the elements in the array> >for> (>int> i = 0; i <3; ++i) {> >printf>('%d ', myArray[i]);> >}> >return> 0;> }> |
>
>
ジャワ
public> class> ArrayExample {> >public> static> void> main(String[] args) {> >// Creating an array> >int>[] myArray = {>10>,>20>,>30>};> >// Accessing elements by index> >int> firstElement = myArray[>0>];> >int> secondElement = myArray[>1>];> >// Modifying an element> >myArray[>1>] =>25>;> >// Displaying the elements in the array> >for> (>int> i =>0>; i <>3>; ++i) {> >System.out.print(myArray[i] + ' ');> >}> >}> }> |
>
>
Python3
リソース割り当てグラフ
# Creating an array (using a list)> my_array>=> [>10>,>20>,>30>]> # Accessing elements by index> first_element>=> my_array[>0>]> second_element>=> my_array[>1>]> # Modifying an element> my_array[>1>]>=> 25> # Displaying the elements in the array> for> element>in> my_array:> >print>(element, end>=>' ')> |
>
>
C#
using> System;> class> Program> {> >static> void> Main()> >{> >// Creating an array> >int>[] myArray = { 10, 20, 30 };> >// Modifying an element> >myArray[1] = 25;> >// Displaying the elements in the array> >foreach> (>int> element>in> myArray)> >{> >Console.Write(element + ' ');> >}> >}> }> |
>
>
JavaScript
// Creating an array> let myArray = [10, 20, 30];> // Accessing elements by index> let firstElement = myArray[0];> let secondElement = myArray[1];> // Modifying an element> myArray[1] = 25;> // Displaying the elements in the array> for> (let i = 0; i console.log(myArray[i]); }> |
>
>出力
10 25 30>
結論として、配列は次のことを提供します。 固定サイズ 、 連続したメモリ構造 と 効率的な要素アクセス 一方、リストは提供します 動的なサイジング 、 柔軟性 、 そして 組み込みメソッド 操作を容易にするためです。この 2 つのどちらを選択するかはアプリケーションの特定の要件によって異なります。配列は固定サイズおよび直接メモリ アクセスが重要なシナリオに優れており、リストは動的データや多様な操作に有利であることが証明されています。最終的に、各データ構造の固有の機能を理解することで、開発者はプログラミング タスクの要求に基づいて情報に基づいた意思決定を行うことができます。