プライベートな連絡先の最初の情報
送料メール:
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
シーケンス テーブルは、連続した物理アドレスを持つストレージ ユニットを使用してデータ要素を順番に格納する線形構造です。一般に、配列ストレージが使用されます。アレイ上のデータの追加、削除、確認、変更を完了します。
線形テーブルには通常、次のメソッドが含まれます。
パブリッククラスMyArrayList {
プライベートint[]配列;
プライベート int サイズ;
// デフォルトの構築方法では、デフォルトでスペースが割り当てられます。
シーケンスリスト(){}
// シーケンス テーブルの基礎となる容量を指定された容量に設定します
SeqList(int 初期容量){ }
// 新しい要素を追加します。デフォルトでは、配列の最後に追加されます。
パブリック void add(int データ) { }
// pos 位置に要素を追加
パブリック void add(int pos, int data) { }
// 要素が含まれているかどうかを判断します
パブリックブール値 contains(int toFind) { true を返す; }
// 要素に対応する位置を検索します
パブリック int indexOf(int toFind) { 戻り値 -1; }
// pos 位置の要素を取得します
パブリック int get(int pos) { 戻り値 -1; }
// pos 位置の要素を value に設定します
パブリック void set(int 位置、int 値) { }
//最初に出現したキーワードキーを削除します
パブリック void 削除 (int toRemove) { }
// シーケンステーブルの長さを取得する
パブリック int サイズ() { 0 を返す; }
// シーケンステーブルをクリアする
パブリックボイドクリア(){}
// シーケンステーブルを印刷する
パブリック void display() { }
}
次に、上記の方法に従って int 型のシーケンス テーブルを実装します。
- import java.util.Arrays;
- public class MyArrayList {
- private int[] elem;
- private int usedSize;
- private static final int DEFAULT_SIZE = 10;
- public MyArrayList(){
- elem = new int[DEFAULT_SIZE];
- }
- public MyArrayList(int initCapacity){
- elem = new int[initCapacity];
- }
-
- private boolean checkCapacity(){
- if(this.usedSize == elem.length){
- return true;
- }
- return false;
- }
-
- public void display(){
- for (int i = 0; i < this.usedSize; i++) {
- System.out.print(this.elem[i] + " ");
- }
- }
- public void add(int data){
- if(checkCapacity()){
- this.elem = Arrays.copyOf(this.elem,2*elem.length);
- }
- this.elem[this.usedSize] = data;
- this.usedSize++;
- return;
- }
- public void add(int pos,int data){
- if(pos > this.usedSize || pos < 0){
- throw new PosOutOfBoundsException("插入位置错误!");
- }
- if(checkCapacity()){
- this.elem = Arrays.copyOf(this.elem,2*elem.length);
- }
- for (int i = this.usedSize - 1; i >=pos ; i--) {
- elem[i+1] = elem[i];
- }
- this.elem[pos] = data;
- this.usedSize++;
- return;
- }
-
- public boolean contains(int data){
- for (int i = 0; i < this.usedSize; i++) {
- if(this.elem[i] == data){
- return true;
- }
- }
- return false;
- }
-
- public int indexof(int data){
- for (int i = 0; i < this.usedSize; i++) {
- if(this.elem[i] == data){
- return i;
- }
- }
- return -1;
- }
- public int get(int pos){
- if(pos >= this.usedSize || pos < 0){
- throw new PosOutOfBoundsException("输入的位置错误!");
- }
- return this.elem[pos];
- }
- public void set(int pos,int data){
- if(pos >= this.usedSize || pos < 0){
- throw new PosOutOfBoundsException("输入的位置错误!");
- }
- this.elem[pos] = data;
- }
- public int size(){
- return this.usedSize;
- }
- public void remove(int data){
- if(this.contains(data)){
- int pos = this.indexof(data);
- for (int i = pos; i < this.usedSize - 1; i++) {
- this.elem[pos] = this.elem[pos+1];
- }
- this.usedSize--;
- }else{
- throw new PosOutOfBoundsException("没有该元素");
- }
- }
- public void clear(){
- this.usedSize = 0;
- return;
- }
- }
コレクション フレームワークでは、ArrayList は List インターフェイスを実装する通常のクラスです。具体的なフレームワーク図は次のとおりです。
ArrayList のコンストラクター メソッド:
ArrayList(); //パラメータ構築なし
ArrayList(コレクション<? extends E> c); //他のコレクションを使用して ArrayList を構築します
ArrayList(int initialCapacity) //シーケンステーブルの初期容量を指定
コード例:
- public class Test {
- public static void main(String[] args) {
- List<Integer> list1 = new ArrayList<>();//无参构造
- List<Integer> list2 = new ArrayList<>(10);//指定容量
- list2.add(1);
- list2.add(2);
- list2.add(3);
- List<Integer> list3 = new ArrayList<>(list2);//利用其他 Collection 构建 ArrayList
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();//无参构造
- list.add(1);
- list.add(2);
- list.add(3);
- System.out.println(list);
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(1,4);
- System.out.println(list);
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list1 = new ArrayList<>();
- list1.add(4);
- list1.add(5);
- list1.add(6);
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.addAll(list1);
- System.out.println(list);
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.remove(1);
- System.out.println(list);
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.remove(new Integer(2));
- System.out.println(list);
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- System.out.println(list.get(1));
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.set(1,4);
- System.out.println(list);
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.clear();
- System.out.println(list);
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- System.out.println(list.contains(2));
- System.out.println(list.contains(4));
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(1);
- System.out.println(list.indexOf(1));
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- list.add(1);
- System.out.println(list.lastIndexOf(1));
- }
- }
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- System.out.println(list.subList(0,2));
- }
- }
ArrayList は、for ループ + 添字、foreach 拡張ループ、イテレータの使用の 3 つの方法で走査できます。
- public class Test {
- public static void main(String[] args) {
- List<Integer> list = new ArrayList<>();
- list.add(1);
- list.add(2);
- list.add(3);
- //使用fori遍历
- for (int i = 0; i < list.size(); i++) {
- System.out.print(list.get(i));
- }
- System.out.println();
- //使用foreach遍历
- for(Integer integer:list){
- System.out.print(integer);
- }
- System.out.println();
- //使用迭代器遍历
- Iterator<Integer> it = list.listIterator();
- while(it.hasNext()){
- System.out.print(it.next());
- }
- }
- }
操作結果:
ArrayList のシナリオ使用法
トランプのデッキをランダムにシャッフルし、各 5 枚のカードを 3 人に配ります。
アルゴリズムの元のコードの場所:
シャッフルカード · 常に淡水魚/Java の古典的な例 - コード クラウド - オープンソース中国 (gitee.com)
トピックの説明:
コード:
- public class Test {
- public static List<List<Integer>> generate(int numRows) {
- List<List<Integer>> list = new LinkedList<>();
- for (int i = 0; i < numRows; i++) {
- List<Integer> row = new LinkedList<>();
- for (int j = 0; j < i + 1; j++) {
- if (j == 0 || i == j) {
- row.add(1);
- } else {
- row.add(list.get(i - 1).get(j - 1) + list.get(i - 1).get(j));
- }
- }
- list.add(row);
- }
- return list;
- }
-
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int numRows = scanner.nextInt();
- List<List<Integer>> list = generate(numRows);
- for (int i = 0; i < numRows; i++) {
- for (int j = 0; j < i + 1; j++) {
- System.out.print(list.get(i).get(j) + " ");
- }
- System.out.println();
- }
- }
- }
走行結果グラフ: