Java の配列: 特性、使用法、および実際のシナリオ
この記事では、Java の配列の特性 (固定サイズ、効率的なアクセス、型安全性など) を探りながら、ArrayList などの動的コレクション型と比較します。また、製品数量の保存、毎日の温度の変更、生徒の成績の並べ替えなどの実際のシナリオも提供し、Java での配列の実用的なアプリケーションを示します。
Java では、配列は開発者が同じ型の複数の値を 1 つの変数に格納できるようにする基本的なデータ構造です。配列は、固定サイズと直接アクセス機能により、データを管理および操作するための効率的な方法を提供します。この記事では、Java 配列の特性を調査し、ArrayList などの他のコレクション型と比較し、配列が役立つ実際のシナリオを示します。効率的な Java プログラムを構築するには、配列のプロパティとアプリケーションを理解することが不可欠です。
以下は Java の配列特性のリストです:
- 固定サイズ: 配列のサイズは、一度定義すると変更できません。
- 順序付け: 配列は要素を順番に格納します。これは、要素がインデックスによって一定時間内にアクセスできることを意味します。
- 効率: 配列内の要素へのアクセスは一定時間の操作です。配列は単一タイプのデータを格納するため、メモリのオーバーヘッドが非常に低くなります。
- 単一型: Java 配列は型指定されています。つまり、配列宣言で宣言されているのと同じデータ型の要素のみを格納できます。
配列は、リストでありコレクション インターフェイスの一部である ArrayList とは異なります。 Java のインターフェイスは、クラスに似た参照型であり、定数、デフォルト メソッド、静的メソッド、および入れ子になった型のみを含めることができます (チュートリアル ポイント、n. d.)。コレクション インターフェイスの場合、add()、remove()、get()、size() などのメソッドが含まれます (Oracle Doc.、未確認)。これにより、ArrayList、LinkedList、Set クラスなどのさまざまなタイプのリスト クラスがこれらのメソッドを使用できるようになります。
配列はコレクション インターフェイスの一部ではないことに注意してください。言い換えれば、それらに関連付けられたメソッドはありません。
配列を使用する実際のシナリオ
シナリオ 1 店舗内の商品数量:
配列を使用すると、店内のさまざまな製品の数量を追跡できます。たとえば、配列の各要素は特定の製品の数量を表します。
public class Main { public static void main(String[] args) { // Stores product quantities int[] quantities = new int[4]; // Storing product quantities quantities[0] = 50; quantities[1] = 30; quantities[2] = 20; quantities[3] = 40; // Prints the product quantities for (int i = 0; i < quantities.length; i++) { System.out.println("Product " + (i + 1) + " Quantity: " + quantities[i]); } } }
出力:
Product 1 Quantity: 50 Product 2 Quantity: 30 Product 3 Quantity: 20 Product 4 Quantity: 40
シナリオ 2:
配列を使用して毎日の気温を保存および変更できます。
public class Main { public static void main(String[] args) { // Stores daily temperatures int[] temperatures = {68, 70, 75, 72, 69, 71, 73}; // Prints initial temperatures System.out.println("Initial daily temperatures:"); printTemperatures(temperatures); // Modifies temperatures modifyTemperature(temperatures, 2, 78); modifyTemperature(temperatures, 5, 74); // Prints updated temperatures System.out.println("\nUpdated daily temperatures:"); printTemperatures(temperatures); } // Method to print all temperatures public static void printTemperatures(int[] temperatures) { String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday", "Sunday"}; for (int i = 0; i < temperatures.length; i++) { System.out.println(days[i] + ": " + temperatures[i] + "°F"); } } // Method to modify a temperature public static void modifyTemperature(int[] temperatures, int dayIndex, int newTemperature) { if (dayIndex >= 0 && dayIndex < temperatures.length) { temperatures[dayIndex] = newTemperature; } else { System.out.println("Invalid day index!"); } } }
出力:
public class Main { public static void main(String[] args) { // Stores product quantities int[] quantities = new int[4]; // Storing product quantities quantities[0] = 50; quantities[1] = 30; quantities[2] = 20; quantities[3] = 40; // Prints the product quantities for (int i = 0; i < quantities.length; i++) { System.out.println("Product " + (i + 1) + " Quantity: " + quantities[i]); } } }
シナリオ 3:
配列を使用して、特定のクラスの生徒の成績を保存および並べ替えることができます。
Product 1 Quantity: 50 Product 2 Quantity: 30 Product 3 Quantity: 20 Product 4 Quantity: 40
出力
public class Main { public static void main(String[] args) { // Stores daily temperatures int[] temperatures = {68, 70, 75, 72, 69, 71, 73}; // Prints initial temperatures System.out.println("Initial daily temperatures:"); printTemperatures(temperatures); // Modifies temperatures modifyTemperature(temperatures, 2, 78); modifyTemperature(temperatures, 5, 74); // Prints updated temperatures System.out.println("\nUpdated daily temperatures:"); printTemperatures(temperatures); } // Method to print all temperatures public static void printTemperatures(int[] temperatures) { String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday","Saturday", "Sunday"}; for (int i = 0; i < temperatures.length; i++) { System.out.println(days[i] + ": " + temperatures[i] + "°F"); } } // Method to modify a temperature public static void modifyTemperature(int[] temperatures, int dayIndex, int newTemperature) { if (dayIndex >= 0 && dayIndex < temperatures.length) { temperatures[dayIndex] = newTemperature; } else { System.out.println("Invalid day index!"); } } }
要約すると、Java 配列は固定サイズであり、同じ型の複数の値を格納します。インデックスを使用して要素に効率的かつ継続的にアクセスできるため、メモリのオーバーヘッドと速度が懸念されるシナリオに適しています。配列には ArrayList のようなコレクションのような柔軟性はありませんが、順序付けされたデータを効率的に処理するための Java ツールキットの貴重な部分です。
参考文献:
Oracle ドキュメント。 (未確認)。 コレクション (Java SE 21) [Java プラットフォーム、Standard Edition Java API 仕様]。オラクル。 https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/Collection.html/
から取得チュートリアルポイント。 (未確認)。 Java インターフェース。チュートリアルのポイント。 https://www.tutorialspoint.com/java/java_interfaces.htm
から取得元々は、2024 年 10 月 16 日に Level UPcoding によって発行された Medium の Alex.omegapy で公開されました。
以上がJava の配列: 特性、使用法、および実際のシナリオの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











一部のアプリケーションが適切に機能しないようにする会社のセキュリティソフトウェアのトラブルシューティングとソリューション。多くの企業は、内部ネットワークセキュリティを確保するためにセキュリティソフトウェアを展開します。 ...

システムドッキングでのフィールドマッピング処理は、システムドッキングを実行する際に難しい問題に遭遇することがよくあります。システムのインターフェイスフィールドを効果的にマッピングする方法A ...

データベース操作にMyBatis-Plusまたはその他のORMフレームワークを使用する場合、エンティティクラスの属性名に基づいてクエリ条件を構築する必要があることがよくあります。あなたが毎回手動で...

多くのアプリケーションシナリオでソートを実装するために名前を数値に変換するソリューションでは、ユーザーはグループ、特に1つでソートする必要がある場合があります...

intellijideaultimatiateバージョンを使用してスプリングを開始します...

Javaオブジェクトと配列の変換:リスクの詳細な議論と鋳造タイプ変換の正しい方法多くのJava初心者は、オブジェクトのアレイへの変換に遭遇します...

eコマースプラットフォーム上のSKUおよびSPUテーブルの設計の詳細な説明この記事では、eコマースプラットフォームでのSKUとSPUのデータベース設計の問題、特にユーザー定義の販売を扱う方法について説明します。

データベースクエリにTKMYBATISを使用する場合、クエリ条件を構築するためにエンティティクラスの変数名を優雅に取得する方法は一般的な問題です。この記事はピン留めします...
