τα στοιχεία επικοινωνίας μου
Ταχυδρομείο[email protected]
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Ένας πίνακας ακολουθιών είναι μια γραμμική δομή που χρησιμοποιεί μια μονάδα αποθήκευσης με συνεχείς φυσικές διευθύνσεις για την αποθήκευση στοιχείων δεδομένων στη σειρά. Ολοκληρώστε την προσθήκη, διαγραφή, έλεγχο και τροποποίηση δεδομένων στον πίνακα.
Οι γραμμικοί πίνακες γενικά περιλαμβάνουν τις ακόλουθες μεθόδους:
δημόσια τάξη MyArrayList {
ιδιωτικός πίνακας int[];
ιδιωτικό μέγεθος int?
//Η προεπιλεγμένη μέθοδος κατασκευής εκχωρεί χώρο από προεπιλογή
SeqList(){}
// Ορίστε την υποκείμενη χωρητικότητα του πίνακα ακολουθιών στην καθορισμένη χωρητικότητα
SeqList(int initcapacity){ }
// Προσθήκη νέων στοιχείων, από προεπιλογή προστίθενται στο τέλος του πίνακα
δημόσιο κενό πρόσθετο(int data) { }
//Προσθήκη στοιχείου στη θέση θέσης
δημόσια κενή προσθήκη(int pos, int data) { }
// Προσδιορίστε εάν περιέχεται ένα στοιχείο
public boolean περιέχει(int toFind) { return true; }
// Βρείτε τη θέση που αντιστοιχεί σε ένα στοιχείο
public int indexOf(int toFind) { return -1; }
// Λάβετε το στοιχείο στη θέση θέσης
public int get(int pos) { return -1; }
// Ορίστε το στοιχείο στη θέση θέση σε τιμή
δημόσιο σύνολο κενών (int pos, int value) { }
//Διαγραφή της πρώτης εμφάνισης του κλειδιού λέξης-κλειδιού
δημόσιο κενό αφαίρεση(int toRemove) { }
// Λάβετε το μήκος του πίνακα ακολουθιών
public int size() { return 0; }
//Εκκαθάριση του πίνακα ακολουθιών
δημόσιο κενό καθαρό() { }
//Εκτύπωση πίνακα ακολουθιών
δημόσια προβολή κενού() { }
}
Στη συνέχεια, εφαρμόστε έναν πίνακα ακολουθιών τύπου 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 είναι μια συνηθισμένη κλάση που υλοποιεί τη διεπαφή λίστας Το συγκεκριμένο διάγραμμα πλαισίου είναι το εξής:
Μέθοδος κατασκευής στο ArrayList:
ArrayList(); //Χωρίς κατασκευή παραμέτρων
ArrayList(Συλλογή<? extends E> γ) //Χρησιμοποιήστε άλλες Συλλογές για τη δημιουργία 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 μπορεί να διασχιστεί με τρεις τρόπους: για βρόχο + δείκτη, foreach ενισχυμένο βρόχο και χρησιμοποιώντας επαναλήπτες
- 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
Ανακατέψτε μια τράπουλα τυχαία και μοιράστε τα σε τρία άτομα, το καθένα με πέντε χαρτιά.
Η θέση του αρχικού κώδικα του αλγορίθμου:
Περιγραφή θέματος:
Κώδικας:
- 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();
- }
- }
- }
Εκτελούμενο γράφημα αποτελεσμάτων: