.NETヘルプ C# ArrayList(開発者向けの動作方法) Curtis Chau 更新日:7月 28, 2025 Download IronPDF NuGet Download テキストの検索と置換 テキストと画像のスタンプ Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article .NET Framework のコレクション名前空間の一部である ArrayList クラスは、オブジェクトのコレクションを格納するために設計されています。 これは非ジェネリックコレクションであり、任意のデータ型のアイテムを保持できます。 この特徴により、非常に柔軟ですが、ジェネリックコレクションと比較して型の安全性が低くなります。 ArrayList は重複する要素を含むことができ、有効な値が追加または削除されるときに動的にサイズを変更することができます。 この記事では、ArrayList の基本と IronPDF ライブラリの機能について説明します。 ArrayList の基本 ArrayList は本質的に非ジェネリックコレクションであり、任意のデータ型の多数の要素を格納できるため、さまざまなプログラミングシナリオに適した汎用性のある選択肢です。 要素を追加または削除するときに固定サイズの制約がないことがその主な特徴の一つです。 ArrayList は新しい要素を収容するために自動的にサイズを調整し、これは IList インターフェースの実装によって可能になっています。 この動的なサイズ変更は、要素数が一生の間に変化するコレクションを必要とするアプリケーションにとって重要です。 ArrayList をインスタンス化するとき、整数や文字列から複雑なカスタムオブジェクトまで、任意のオブジェクト値を保持できるコレクションを作成しています。 Add などのメソッドのおかげで、ArrayList に要素を追加することは簡単で、コレクションの末尾にオブジェクト値を追加し、Insert は指定されたインデックスに新しいアイテムを配置し、必要に応じて既存の要素をシフトして空間を作ります。 この柔軟性により、開発者はアプリケーションの進化に応じてコレクションをより効果的に管理できます。 要素の操作 さまざまなタイプのデータのコレクションを構築している状況を考えると、ArrayList に要素を追加することは簡単で直感的です。 Add メソッドを使用すると、文字列や整数、あるいは他のコレクションからもArrayList に任意のオブジェクトを追加できます。 ArrayList の容量は必要に応じて自動的に増加し、新しいオブジェクトの要素のためのスペースが常に確保されます。 この自動サイズ変更は、新しい要素を収容するために手動でサイズを変更する必要がある従来の配列に対する大きな利点です。 ArrayList はまた、特定の位置または整数インデックスで要素を挿入および削除するためのメソッドを提供します。 Insert メソッドを使用すると、指定された位置に要素を追加でき、任意のインデックスに新しいアイテムを正確に配置することが可能です。 Remove と RemoveAt メソッドは、削除対象のオブジェクトまたはコレクション内のそのインデックスを指定することにより、アイテムの削除を迅速に行います。 この細やかなコントロールは、ArrayList を動的データを管理するための強力なツールにしています。 要素の作成と追加 ArrayList を使用し始めるには、まずそのインスタンスを作成する必要があります。 その後、Add メソッドを使用して、ArrayList の末尾にオブジェクトを追加することができます。 using System; using System.Collections; class Program { // The main entry point of the program public static void Main() { // Create a new ArrayList ArrayList myArrayList = new ArrayList(); // Add elements of different types myArrayList.Add("Hello"); myArrayList.Add(100); var item = "World"; myArrayList.Add(item); // Iterate through the ArrayList and print each element foreach (var obj in myArrayList) { Console.WriteLine(obj); } } } using System; using System.Collections; class Program { // The main entry point of the program public static void Main() { // Create a new ArrayList ArrayList myArrayList = new ArrayList(); // Add elements of different types myArrayList.Add("Hello"); myArrayList.Add(100); var item = "World"; myArrayList.Add(item); // Iterate through the ArrayList and print each element foreach (var obj in myArrayList) { Console.WriteLine(obj); } } } Imports System Imports System.Collections Friend Class Program ' The main entry point of the program Public Shared Sub Main() ' Create a new ArrayList Dim myArrayList As New ArrayList() ' Add elements of different types myArrayList.Add("Hello") myArrayList.Add(100) Dim item = "World" myArrayList.Add(item) ' Iterate through the ArrayList and print each element For Each obj In myArrayList Console.WriteLine(obj) Next obj End Sub End Class $vbLabelText $csharpLabel この例は、新しい ArrayList を作成し、異なるタイプの要素を追加する方法を示しています。 foreach ループは ArrayList を通じて各要素を反復処理し、プリントします。 要素の挿入 要素を指定されたインデックスに挿入するには、ゼロベースのインデックスシステムであることに注意しながら Insert メソッドを使用します。 // Insert element at index 1 myArrayList.Insert(1, "Inserted Item"); // Insert element at index 1 myArrayList.Insert(1, "Inserted Item"); ' Insert element at index 1 myArrayList.Insert(1, "Inserted Item") $vbLabelText $csharpLabel 要素の削除 要素を削除するために、Remove と RemoveAt メソッドは役立ちます。 Remove は特定のオブジェクトの最初の出現を削除し、RemoveAt は指定された整数インデックスの要素を削除します。 myArrayList.Remove("Hello"); // Removes the first occurrence of "Hello" myArrayList.RemoveAt(0); // Removes the element at index 0 myArrayList.Remove("Hello"); // Removes the first occurrence of "Hello" myArrayList.RemoveAt(0); // Removes the element at index 0 myArrayList.Remove("Hello") ' Removes the first occurrence of "Hello" myArrayList.RemoveAt(0) ' Removes the element at index 0 $vbLabelText $csharpLabel 例: ArrayList の管理 C# で ArrayList を使用する高度な例を作成するには、要素の追加や削除のような基本操作だけでなく、ソートや検索、ArrayList を他のデータ構造に変換するなどのより複雑な操作も紹介します。 以下の例を Program.cs ファイルに追加して実行してください。 using System; using System.Collections; using System.Linq; class AdvancedArrayListExample { static void Main(string[] args) { // Initialize an ArrayList with some elements ArrayList numbers = new ArrayList() { 5, 8, 1, 3, 2 }; // Adding elements numbers.Add(6); // Add an element to the end numbers.AddRange(new int[] { 7, 9, 0 }); // Add multiple elements from a specified collection. Console.WriteLine("Initial ArrayList:"); foreach (int number in numbers) { Console.Write(number + " "); } Console.WriteLine("\n"); // Removing elements numbers.Remove(1); // Remove the element 1 numbers.RemoveAt(0); // Remove the first element Console.WriteLine("After Removal:"); foreach (int number in numbers) { Console.Write(number + " "); } Console.WriteLine("\n"); // Sorting numbers.Sort(); // Sort the ArrayList Console.WriteLine("Sorted ArrayList:"); foreach (int number in numbers) { Console.Write(number + " "); } Console.WriteLine("\n"); // Searching int searchFor = 5; int index = numbers.IndexOf(searchFor); // Find the index of the element if (index != -1) { Console.WriteLine($"Element {searchFor} found at index {index}"); } else { Console.WriteLine($"Element {searchFor} not found."); } Console.WriteLine("\n"); // Converting ArrayList to Array int[] numbersArray = (int[])numbers.ToArray(typeof(int)); Console.WriteLine("Converted Array:"); foreach (int number in numbersArray) { Console.Write(number + " "); } Console.WriteLine("\n"); // Demonstrate LINQ with ArrayList (Requires System.Linq) var evenNumbers = numbers.Cast<int>().Where(n => n % 2 == 0).ToList(); // Assign values to evenNumbers from the filtered results. Console.WriteLine("Even Numbers:"); evenNumbers.ForEach(n => Console.Write(n + " ")); Console.WriteLine(); } } using System; using System.Collections; using System.Linq; class AdvancedArrayListExample { static void Main(string[] args) { // Initialize an ArrayList with some elements ArrayList numbers = new ArrayList() { 5, 8, 1, 3, 2 }; // Adding elements numbers.Add(6); // Add an element to the end numbers.AddRange(new int[] { 7, 9, 0 }); // Add multiple elements from a specified collection. Console.WriteLine("Initial ArrayList:"); foreach (int number in numbers) { Console.Write(number + " "); } Console.WriteLine("\n"); // Removing elements numbers.Remove(1); // Remove the element 1 numbers.RemoveAt(0); // Remove the first element Console.WriteLine("After Removal:"); foreach (int number in numbers) { Console.Write(number + " "); } Console.WriteLine("\n"); // Sorting numbers.Sort(); // Sort the ArrayList Console.WriteLine("Sorted ArrayList:"); foreach (int number in numbers) { Console.Write(number + " "); } Console.WriteLine("\n"); // Searching int searchFor = 5; int index = numbers.IndexOf(searchFor); // Find the index of the element if (index != -1) { Console.WriteLine($"Element {searchFor} found at index {index}"); } else { Console.WriteLine($"Element {searchFor} not found."); } Console.WriteLine("\n"); // Converting ArrayList to Array int[] numbersArray = (int[])numbers.ToArray(typeof(int)); Console.WriteLine("Converted Array:"); foreach (int number in numbersArray) { Console.Write(number + " "); } Console.WriteLine("\n"); // Demonstrate LINQ with ArrayList (Requires System.Linq) var evenNumbers = numbers.Cast<int>().Where(n => n % 2 == 0).ToList(); // Assign values to evenNumbers from the filtered results. Console.WriteLine("Even Numbers:"); evenNumbers.ForEach(n => Console.Write(n + " ")); Console.WriteLine(); } } Imports Microsoft.VisualBasic Imports System Imports System.Collections Imports System.Linq Friend Class AdvancedArrayListExample Shared Sub Main(ByVal args() As String) ' Initialize an ArrayList with some elements Dim numbers As New ArrayList() From { 5, 8, 1, 3, 2 } ' Adding elements numbers.Add(6) ' Add an element to the end numbers.AddRange(New Integer() { 7, 9, 0 }) ' Add multiple elements from a specified collection. Console.WriteLine("Initial ArrayList:") For Each number As Integer In numbers Console.Write(number & " ") Next number Console.WriteLine(vbLf) ' Removing elements numbers.Remove(1) ' Remove the element 1 numbers.RemoveAt(0) ' Remove the first element Console.WriteLine("After Removal:") For Each number As Integer In numbers Console.Write(number & " ") Next number Console.WriteLine(vbLf) ' Sorting numbers.Sort() ' Sort the ArrayList Console.WriteLine("Sorted ArrayList:") For Each number As Integer In numbers Console.Write(number & " ") Next number Console.WriteLine(vbLf) ' Searching Dim searchFor As Integer = 5 Dim index As Integer = numbers.IndexOf(searchFor) ' Find the index of the element If index <> -1 Then Console.WriteLine($"Element {searchFor} found at index {index}") Else Console.WriteLine($"Element {searchFor} not found.") End If Console.WriteLine(vbLf) ' Converting ArrayList to Array Dim numbersArray() As Integer = DirectCast(numbers.ToArray(GetType(Integer)), Integer()) Console.WriteLine("Converted Array:") For Each number As Integer In numbersArray Console.Write(number & " ") Next number Console.WriteLine(vbLf) ' Demonstrate LINQ with ArrayList (Requires System.Linq) Dim evenNumbers = numbers.Cast(Of Integer)().Where(Function(n) n Mod 2 = 0).ToList() ' Assign values to evenNumbers from the filtered results. Console.WriteLine("Even Numbers:") evenNumbers.ForEach(Sub(n) Console.Write(n & " ")) Console.WriteLine() End Sub End Class $vbLabelText $csharpLabel このコードスニペットでは以下を示します: 要素のセットを持つ ArrayList を初期化する。 ArrayList に単一および複数の要素を追加する。 値およびインデックスで要素を削除する。 要素を順序付けるために ArrayList をソートする。 要素を検索し、そのインデックスを見つける。 ArrayList を標準配列に変換する。 非ジェネリックコレクションを LINQ の強力なクエリ機能と連携させる方法を示すために、ArrayList で LINQ を使用して偶数をフィルタリングする。 イントロダクション: IronPDF - C# PDF ライブラリ IronPDF は C# 用の強力なライブラリで、PDF の生成プロセスを簡素化し、HTML からの PDF 生成、テキストや画像の追加、文書の保護など、多くのPDF操作機能を提供します。 IronPDF と ArrayList の統合 アイテムのArrayListを作成し、それを使用してIronPDFを使用してそれらのアイテムのPDF文書を生成する簡単なC#プログラムを書いてみましょう。 using IronPdf; using System; using System.Collections; class PdfCode { static void Main(string[] args) { // Set your IronPDF license key here IronPdf.License.LicenseKey = "Your_License_Key"; // Create a new ArrayList and add some items ArrayList itemList = new ArrayList(); itemList.Add("Apple"); itemList.Add("Banana"); itemList.Add("Cherry"); itemList.Add("Date"); // Initialize a new PDF document var Renderer = new ChromePdfRenderer(); // Create an HTML string to hold our content string htmlContent = "<h1>Items List</h1><ul>"; // Iterate over each item in the ArrayList and add it to the HTML string foreach (var item in itemList) { htmlContent += $"<li>{item}</li>"; } htmlContent += "</ul>"; // Convert the HTML string to a PDF document var PDF = Renderer.RenderHtmlAsPdf(htmlContent); // Save the PDF to a file PDF.SaveAs("ItemList.pdf"); Console.WriteLine("PDF file 'ItemList.pdf' has been generated."); } } using IronPdf; using System; using System.Collections; class PdfCode { static void Main(string[] args) { // Set your IronPDF license key here IronPdf.License.LicenseKey = "Your_License_Key"; // Create a new ArrayList and add some items ArrayList itemList = new ArrayList(); itemList.Add("Apple"); itemList.Add("Banana"); itemList.Add("Cherry"); itemList.Add("Date"); // Initialize a new PDF document var Renderer = new ChromePdfRenderer(); // Create an HTML string to hold our content string htmlContent = "<h1>Items List</h1><ul>"; // Iterate over each item in the ArrayList and add it to the HTML string foreach (var item in itemList) { htmlContent += $"<li>{item}</li>"; } htmlContent += "</ul>"; // Convert the HTML string to a PDF document var PDF = Renderer.RenderHtmlAsPdf(htmlContent); // Save the PDF to a file PDF.SaveAs("ItemList.pdf"); Console.WriteLine("PDF file 'ItemList.pdf' has been generated."); } } Imports IronPdf Imports System Imports System.Collections Friend Class PdfCode Shared Sub Main(ByVal args() As String) ' Set your IronPDF license key here IronPdf.License.LicenseKey = "Your_License_Key" ' Create a new ArrayList and add some items Dim itemList As New ArrayList() itemList.Add("Apple") itemList.Add("Banana") itemList.Add("Cherry") itemList.Add("Date") ' Initialize a new PDF document Dim Renderer = New ChromePdfRenderer() ' Create an HTML string to hold our content Dim htmlContent As String = "<h1>Items List</h1><ul>" ' Iterate over each item in the ArrayList and add it to the HTML string For Each item In itemList htmlContent &= $"<li>{item}</li>" Next item htmlContent &= "</ul>" ' Convert the HTML string to a PDF document Dim PDF = Renderer.RenderHtmlAsPdf(htmlContent) ' Save the PDF to a file PDF.SaveAs("ItemList.pdf") Console.WriteLine("PDF file 'ItemList.pdf' has been generated.") End Sub End Class $vbLabelText $csharpLabel この例では、まず itemList という名前の ArrayList を作成し、いくつかの文字列アイテムでそれを埋めます。 次に、IronPDF の ChromePdfRenderer クラスの新しいインスタンスを初期化し、これを使用して HTML コンテンツを PDF 文書に変換します。 出力 IronPDF によって生成された出力 PDF は次のとおりです: 結論 ArrayList はオブジェクトのリストを格納するために C# が提供する強力なコレクションです。 サイズを動的に調整し、任意の型の要素を格納する能力により、広範囲のアプリケーションにとって汎用性があります。 しかし、型の安全性と性能向上のためには、ジェネリックコレクションが推奨されます。 ArrayListとそのメソッドを試すことで、その用途とアプリケーションにどのように適合するかを理解することができます。 さらに、C#の能力をPDF操作に拡張することに関心がある方のために、IronPDFはその機能を探求するために PDF Functions in .NET の無料トライアルを提供します。 ライセンスは $799 から始まり、.NETアプリケーションにPDF機能を統合するための包括的なソリューションを提供します。 よくある質問 C#でArrayListをPDFに変換する方法は? C#でArrayListからPDFを生成するには、IronPDFを使用できます。ArrayListを反復して、PDF生成に適した形式に内容をコンパイルし、その後IronPDFのメソッドを使用してPDFを作成し保存します。 ArrayListを使用する際のIronPDFの利点は何ですか? IronPDFを使用すると、開発者はArrayListに格納されたデータを簡単にPDFドキュメントに変換できます。これは、最小限のコードで最大限の効率を持って、レポートを作成したり、アイテムのリストをエクスポートしたりするのに役立ちます。 ArrayListから生成されたPDFにテキストと画像を追加できますか? はい、IronPDFを使用すると、ArrayList内のアイテムを反復しながら、テキスト、画像、その他のコンテンツを追加してPDFをカスタマイズできます。 C#でArrayListから生成されたPDFを保護することは可能ですか? IronPDFはPDFドキュメントを保護する機能を提供します。ArrayListのデータから生成されたPDFのアクセスや編集を制限するために、パスワードや権限を設定できます。 PDFライブラリと統合する際にArrayListの動的リサイズがどのように役立つのか? ArrayListの動的リサイズにより、容量を気にせずに必要に応じて要素を追加または削除できます。この柔軟性は、IronPDFのようなライブラリを使用してPDFを生成する際にデータを準備する際に有用です。 C#開発者にとってIronPDFを使用する利点は何ですか? IronPDFはC#開発者にPDFドキュメントを生成し操作するための強力なツールセットを提供します。HTMLからPDFへの変換、注釈の追加、複数のPDFのマージなど、さまざまな機能をサポートしており、.NETアプリケーションにとって不可欠なライブラリです。 PDFを作成する際に、ArrayListで異なるデータ型をどのように処理できますか? ArrayListは任意のデータ型を格納できるため、IronPDFを使用してこれらの多様なデータ型を反復し、必要な変換を適用して一貫性のあるPDFドキュメントにフォーマットおよび変換できます。 ArrayListを使用したIronPDFのトラブルシューティングのヒントは? PDFに変換する前にArrayList内のデータが正しくフォーマットされていることを確認してください。null値や互換性のないデータ型をチェックし、IronPDFのデバッグツールを使用して、PDF生成プロセス中に発生した問題を特定し、解決してください。 Curtis Chau 今すぐエンジニアリングチームとチャット テクニカルライター Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。 関連する記事 更新日 9月 4, 2025 RandomNumberGenerator C# RandomNumberGenerator C#クラスを使用すると、PDF生成および編集プロジェクトを次のレベルに引き上げることができます 詳しく読む 更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む 更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む Math.Round C#(開発者向けの動作方法)C# Linter(開発者向けの動...
更新日 9月 4, 2025 RandomNumberGenerator C# RandomNumberGenerator C#クラスを使用すると、PDF生成および編集プロジェクトを次のレベルに引き上げることができます 詳しく読む
更新日 9月 4, 2025 C# String Equals(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む
更新日 8月 5, 2025 C# Switch Pattern Matching(開発者向けの仕組み) 強力なPDFライブラリであるIronPDFと組み合わせることで、switchパターンマッチングは、ドキュメント処理のためのよりスマートでクリーンなロジックを構築できます 詳しく読む