2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
A sequential list is a linear structure that uses a continuous physical address storage unit to store data elements in sequence. Generally, it uses an array to store data. Add, delete, query, and modify data on the array.
Linear tables generally include the following methods:
public class MyArrayList {
private int[] array;
private int size;
// The default constructor allocates space by default
SeqList(){ }
// Set the underlying capacity of the sequence table to the specified capacity
SeqList(int initcapacity){ }
// Add new elements, added at the end of the array by default
public void add(int data) { }
// Add an element at position pos
public void add(int pos, int data) { }
// Determine whether an element is contained
public boolean contains(int toFind) { return true; }
// Find the position corresponding to an element
public int indexOf(int toFind) { return -1; }
// Get the element at position pos
public int get(int pos) { return -1; }
// Set the element at position pos to value
public void set(int pos, int value) { }
//Delete the first occurrence of the keyword key
public void remove(int toRemove) { }
// Get the length of the sequence table
public int size() { return 0; }
// Clear the sequence table
public void clear() { }
// Print order table
public void display() { }
}
Next, implement a sequence table of int type according to the above method:
- 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;
- }
- }
In the collection framework, ArrayList is a common class that implements the List interface. The specific framework diagram is as follows:
Constructor in ArrayList:
ArrayList(); //Construction without parameters
ArrayList(Collection<? extends E> c); //Use other Collections to build ArrayList
ArrayList(int initialCapacity); //Specify the initial capacity of the sequence list
Code example:
- 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 can be traversed in three ways: for loop + subscript, foreach enhanced loop, and using iterators
- 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());
- }
- }
- }
operation result:
ArrayList usage scenarios
A deck of playing cards is randomly shuffled and distributed to three people, with five cards each.
The original code of the algorithm is located at:
shufflecards · A freshwater fish/Java classic example - Code Cloud - Open Source China (gitee.com)
Description of the topic:
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();
- }
- }
- }
Running result chart: