标签:eager stat builder 设置 函数 一半 次方 包装 因此
public class Main { public static void main(String[] args) { int[] arr = new int[10]; for (int i = 0; i < arr.length; i++) arr[i] = i; } }
public class Main { public static void main(String[] args) { int[] scores = new int[]{100, 99, 86}; for (int i = 0; i < scores.length; i++) System.out.println(scores[i]); } }
public class Main { public static void main(String[] args) { int[] scores = new int[]{100, 99, 86}; for (int score : scores) System.out.println(score); } }
socres[1] = 98;
2.. 实现自定义数组类Array所包含的基本方法:
public class Array { private int[] data; //设置为private,不希望用户从外部直接获取这些信息,防止用户篡改数据 private int size; //构造函数,传入数组的容量capacity构造Array public Array(int capacity) { data = new int[capacity]; size = 0; } //无参数构造函数,默认数组容量capacity=10 public Array() { this(10); //这里的capacity是IDE自动添加的提示信息,实际不存在 } //获取数组中的元素个数 public int getSize() { return size; } //获取数组的容量 public int getCapacity() { return data.length; } //判断数组是否为空 public boolean isEmpty() { return size == 0; } }
3.. 实现向自定义数组中添加元素的方法
//向数组末尾添加一个新元素 public void addLast(int e) { if (size == data.length) { throw new IllegalArgumentException("AddLast failed. Array is full."); } data[size] = e; size++; }
//在index位置插入一个新元素e public void add(int index, int e) { if (size == data.length) { throw new IllegalArgumentException("Add failed. Array is full."); } if (index < 0 || index > size) { throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size"); } for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = e; size++; }
//向数组末尾添加一个新元素 public void addLast(int e) { add(size, e); }
//向数组开头添加一个新元素 public void addFirst(int e) { add(0, e); }
4.. 实现在数组中查询元素和修改元素的方法
@Override public String toString() { //覆盖父类的toString方法 StringBuilder res = new StringBuilder(); res.append(String.format("Array: size=%d, capacity=%d\n", size, data.length)); res.append(‘[‘); for (int i = 0; i < size; i++) { res.append(data[i]); if (i != size - 1) { res.append(", "); } } res.append(‘]‘); return res.toString(); }
public class Main { public static void main(String[] args) { Array arr = new Array(20); for (int i = 0; i < 10; i++) { arr.addLast(i); //测试addLast方法 } System.out.println(arr); arr.add(1, 100); //测试add方法 System.out.println(arr); arr.addFirst(-1); //测试addFirst方法 System.out.println(arr); } }
Array: size=10, capacity=20 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=11, capacity=20 [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=12, capacity=20 [-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
//获取index位置的元素 public int get(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Get failed. Index is illegal."); } return data[index]; }
//修改index位置的元素为e public void set(int index, int e) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Set failed. Index is illegal."); } data[index] = e; }
//查找数组中是否存在元素e public boolean contains(int e) { for (int i = 0; i < size; i++) { if (data[i] == e) { return true; } } return false; }
//查看数组中元素e的索引,若找不到元素e,返回-1 public int find(int e){ for(int i=0;i<size;i++){ if(data[i] == e){ return i; } } return -1; }
//删除掉index位置的元素,并且返回所删除的元素 public int remove(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Add failed. Require index >= 0 and index < size"); } int ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size--; return ret; } //删除掉数组开头的元素,并返回所删除的元素 public int removeFirst() { return remove(0); } //删除掉数组末尾的元素,并返回所删除的元素 public int removeLast() { return remove(size - 1); } //如果数组中有元素e,那么将其删除,否则什么也不做 public void removeElement(int e) { int index = find(e); if (index != -1) { remove(index); } }
5.. 整理我们目前实现的业务逻辑:
public class Array { private int[] data; //设置为private,不希望用户从外部直接获取这些信息,防止用户篡改数据 private int size; //构造函数,传入数组的容量capacity构造Array public Array(int capacity) { data = new int[capacity]; size = 0; } //无参数构造函数,默认数组容量capacity=10 public Array() { this(10); //这里的capacity是IDE自动添加的提示信息,实际不存在 } //获取数组中的元素个数 public int getSize() { return size; } //获取数组的容量 public int getCapacity() { return data.length; } //判断数组是否为空 public boolean isEmpty() { return size == 0; } //向数组末尾添加一个新元素e public void addLast(int e) { add(size, e); } //向数组开头添加一个新元素e public void addFirst(int e) { add(0, e); } //在index位置插入一个新元素e public void add(int index, int e) { if (size == data.length) { throw new IllegalArgumentException("Add failed. Array is full."); } if (index < 0 || index > size) { throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size"); } for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = e; size++; } //获取index位置的元素 public int get(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Get failed. Index is illegal."); } return data[index]; } //修改index位置的元素为e public void set(int index, int e) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Set failed. Index is illegal."); } data[index] = e; } //查找数组中是否存在元素e public boolean contains(int e) { for (int i = 0; i < size; i++) { if (data[i] == e) { return true; } } return false; } //查看数组中元素e的索引,若找不到元素e,返回-1 public int find(int e) { for (int i = 0; i < size; i++) { if (data[i] == e) { return i; } } return -1; } //删除掉index位置的元素,并且返回删除的元素 public int remove(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Remove failed. Index is illegal."); } int ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size--; return ret; } //删除掉数组开头的元素,并返回删除的元素 public int removeFirst() { return remove(0); } //删除掉数组末尾的元素,并返回删除的元素 public int removeLast() { return remove(size - 1); } //如果数组中有元素e,那么将其删除,否则什么也不做 public void removeElement(int e) { int index = find(e); if (index != -1) { remove(index); } } @Override public String toString() { //覆盖父类的toString方法 StringBuilder res = new StringBuilder(); res.append(String.format("Array: size=%d, capacity=%d\n", size, data.length)); res.append(‘[‘); for (int i = 0; i < size; i++) { res.append(data[i]); if (i != size - 1) { res.append(", "); } } res.append(‘]‘); return res.toString(); } }
6.. 现在我们的自定义数组的元素只允许为int类型,我们需要进行优化,让数组可以放置"任意"类型的元素,解决这个问题的技术称之为"泛型"。这里的"任意"加了引号,这是因为在Java中一个泛型类并不能放置任意数据类型的元素,泛型不能放置基本数据类型,只能放置类对象。在Java中,共有8中基本数据类型:int、short、long、boolean、byte、char、float、double。如果将数组设置成泛型数组,那么就无法放置这些基本数据类型了。为了解决这个问题,Java语言为每个基本数据类型都设计了一个包装类,把本来不是类对象的数据包装成了类对象。这8中基本数据类型所对应的包装类分别为:Int、Short、Long、Boolean、Byte、Char、Float、Double,在需要的情况下,包装类与其对应的基本数据类型可以自动进行转换。
public class Array<E> { private E[] data; //设置为private,不希望用户从外部直接获取这些信息,防止用户篡改数据 private int size; //构造函数,传入数组的容量capacity构造Array public Array(int capacity) { data = (E[]) new Object[capacity]; size = 0; } //无参数构造函数,默认数组容量capacity=10 public Array() { this(10); //这里的capacity是IDE自动添加的提示信息,实际不存在 } //获取数组中的元素个数 public int getSize() { return size; } //获取数组的容量 public int getCapacity() { return data.length; } //判断数组是否为空 public boolean isEmpty() { return size == 0; } //向数组末尾添加一个新元素e public void addLast(E e) { add(size, e); } //向数组开头添加一个新元素e public void addFirst(E e) { add(0, e); } //在index位置插入一个新元素e public void add(int index, E e) { if (size == data.length) { throw new IllegalArgumentException("Add failed. Array is full."); } if (index < 0 || index > size) { throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size"); } for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = e; size++; } //获取index位置的元素 public E get(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Get failed. Index is illegal."); } return data[index]; } //修改index位置的元素为e public void set(int index, E e) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Set failed. Index is illegal."); } data[index] = e; } //查找数组中是否存在元素e public boolean contains(E e) { for (int i = 0; i < size; i++) { if (data[i].equals(e)) { return true; } } return false; } //查看数组中元素e的索引,若找不到元素e,返回-1 public int find(E e) { for (int i = 0; i < size; i++) { if (data[i].equals(e)) { return i; } } return -1; } //删除掉index位置的元素,并且返回删除的元素 public E remove(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Remove failed. Index is illegal."); } E ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size--; //data[size]会指向一个类对象,这部分空间不会被释放loitering objects data[size] = null; return ret; } //删除掉数组开头的元素,并返回删除的元素 public E removeFirst() { return remove(0); } //删除掉数组末尾的元素,并返回删除的元素 public E removeLast() { return remove(size - 1); } //如果数组中有元素e,那么将其删除,否则什么也不做 public void removeElement(E e) { int index = find(e); if (index != -1) { remove(index); } } @Override public String toString() { //覆盖父类的toString方法 StringBuilder res = new StringBuilder(); res.append(String.format("Array: size=%d, capacity=%d\n", size, data.length)); res.append(‘[‘); for (int i = 0; i < size; i++) { res.append(data[i]); if (i != size - 1) { res.append(", "); } } res.append(‘]‘); return res.toString(); } }
8.. 再次在main方法中实例化我们的自定义数组,进行测试:
public class Main { public static void main(String[] args) { Array<Integer> arr = new Array<>(20); for (int i = 0; i < 10; i++) { arr.addLast(i); } System.out.println(arr); arr.add(1, 100); System.out.println(arr); arr.addFirst(-1); System.out.println(arr); arr.remove(2); System.out.println(arr); arr.removeElement(4); System.out.println(arr); arr.removeFirst(); System.out.println(arr); arr.removeLast(); System.out.println(arr); } }
Array: size=10, capacity=20 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=11, capacity=20 [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=12, capacity=20 [-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=11, capacity=20 [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=10, capacity=20 [-1, 0, 1, 2, 3, 5, 6, 7, 8, 9] Array: size=9, capacity=20 [0, 1, 2, 3, 5, 6, 7, 8, 9] Array: size=8, capacity=20 [0, 1, 2, 3, 5, 6, 7, 8]
9.. 测试让自定义数组去承载对象
public class Student { private String name; private int score; public Student(String studetName, int studentScore) { name = studetName; score = studentScore; } @Override public String toString() { return String.format("Student(name: %s, score: %d)", name, score); } public static void main(String[] args) { Array<Student> arr = new Array<>(); arr.addLast(new Student("XueZou", 98)); arr.addLast(new Student("Guiche", 100)); arr.addLast(new Student("QUiShui", 99)); System.out.println(arr); } }
Array: size=3, capacity=10
[Student(name: XueZou, score: 98), Student(name: Guiche, score: 100), Student(name: QUiShui, score: 99)]
//在index位置插入一个新元素e public void add(int index, E e) { if (index < 0 || index > size) { throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size"); } if (size == data.length) { resize(2 * size); //扩大为原容量的2倍 } for (int i = size - 1; i >= index; i--) { data[i + 1] = data[i]; } data[index] = e; size++; }
private void resize(int newCapacity) { E[] newData = (E[]) new Object[newCapacity]; for (int i = 0; i < size; i++) { newData[i] = data[i]; } data = newData; }
public static void main(String[] args) { Array<Integer> arr = new Array<>(); for (int i = 0; i < 10; i++) { arr.addLast(i); } System.out.println(arr); arr.add(1, 100); System.out.println(arr); arr.addFirst(-1); System.out.println(arr); }
Array: size=10, capacity=10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=11, capacity=20 [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=12, capacity=20 [-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9]
//删除掉index位置的元素,并且返回删除的元素 public E remove(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Remove failed. Index is illegal."); } E ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size--; //data[size]会指向一个类对象,这部分空间不会被释放loitering objects data[size] = null; if (size == data.length / 2) { resize(data.length / 2); //被利用的空间等于总空间的一半时,将数组容量减少一半 } return ret; }
public static void main(String[] args) { Array<Integer> arr = new Array<>(); for (int i = 0; i < 10; i++) { arr.addLast(i); } System.out.println(arr); arr.add(1, 100); System.out.println(arr); arr.addFirst(-1); System.out.println(arr); arr.remove(2); System.out.println(arr); arr.removeElement(4); System.out.println(arr); arr.removeFirst(); System.out.println(arr); }
Array: size=10, capacity=10 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=11, capacity=20 [0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=12, capacity=20 [-1, 0, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=11, capacity=20 [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Array: size=10, capacity=10 [-1, 0, 1, 2, 3, 5, 6, 7, 8, 9] Array: size=9, capacity=10 [0, 1, 2, 3, 5, 6, 7, 8, 9]
public static int sum(int[] nums) { int sum = 0; for (int num: nums) { sum += num; return sum; }
public E remove(int index) { if (index < 0 || index >= size) { throw new IllegalArgumentException("Remove failed. Index is illegal."); } E ret = data[index]; for (int i = index + 1; i < size; i++) { data[i - 1] = data[i]; } size--; //data[size]会指向一个类对象,这部分空间不会被释放loitering objects data[size] = null; if (size == data.length / 4 && data.length / 2 != 0) { //改动在这里 resize(data.length / 2); //被利用的空间等于总空间的一半时,将数组容量减少一半 } return ret; }
标签:eager stat builder 设置 函数 一半 次方 包装 因此
原文地址:https://www.cnblogs.com/xuezou/p/9276945.html