2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Une table de séquence est une structure linéaire qui utilise une unité de stockage avec une adresse physique continue pour stocker des éléments de données en séquence. Généralement, le stockage en tableau est utilisé. Terminez l'ajout, la suppression, la vérification et la modification des données sur la baie.
Les tableaux linéaires incluent généralement les méthodes suivantes :
classe publique MyArrayList {
tableau privé int[] ;
taille int privée ;
//La méthode de construction par défaut alloue de l'espace par défaut
Liste de séquences(){ }
// Définit la capacité sous-jacente de la table de séquence sur la capacité spécifiée
SeqList(int initcapacity){ }
// Ajoute de nouveaux éléments, par défaut ils sont ajoutés à la fin du tableau
public void add(int données) { }
//Ajouter un élément à la position pos
public void add(int pos, int données) { }
// Détermine si un élément est contenu
public boolean contient(int toFind) { renvoie vrai; }
// Trouver la position correspondant à un élément
public int indexOf(int toFind) { return -1; }
// Récupère l'élément en position pos
public int get(int pos) { return -1; }
// Définit l'élément en position pos sur la valeur
public void set(int pos, int valeur) { }
//Supprime la première occurrence du mot-clé key
public void remove(int toRemove) { }
// Récupère la longueur de la table de séquence
public int size() { return 0; }
//Effacer la table de séquence
public void clear() { }
//Imprimer la table de séquence
public void affichage() { }
}
Ensuite, implémentez une table de séquence de type int selon la méthode ci-dessus :
- 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;
- }
- }
Dans le framework de collection, ArrayList est une classe ordinaire qui implémente l'interface List. Le diagramme du framework spécifique est le suivant :
Méthode constructeur dans ArrayList :
ArrayList(); //Aucune construction de paramètre
TableauListe(Collection<? extends E> c); //Utiliser d'autres collections pour créer ArrayList
ArrayList(int initialCapacity); //Spécifiez la capacité initiale de la table de séquence
Exemple de code :
- 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 peut être parcouru de trois manières : boucle for + indice, boucle améliorée foreach et utilisation d'itérateurs
- 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());
- }
- }
- }
résultat de l'opération :
Utilisation de scénario d'ArrayList
Mélangez un jeu de cartes au hasard et distribuez-les à trois personnes, chacune avec cinq cartes.
L'emplacement du code original de l'algorithme :
Description du sujet:
Code:
- 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();
- }
- }
- }
Graphique des résultats d'exécution :