一、數(shù)據(jù)結(jié)構(gòu)
- 是一門(mén)基礎(chǔ)學(xué)科
- 研究的是數(shù)據(jù)如何在計(jì)算機(jī)中進(jìn)行組織和存儲(chǔ),使得我們可以高效的獲取數(shù)據(jù)和修改數(shù)據(jù)
- 數(shù)據(jù)結(jié)構(gòu)可以分為三類(lèi): 線性結(jié)構(gòu): 數(shù)組、隊(duì)列、棧、鏈表、哈希表… 樹(shù)型結(jié)構(gòu):二叉樹(shù)、二分搜索樹(shù)、AVL樹(shù),紅黑樹(shù)、堆、Trie、線段樹(shù)、并查集… 圖結(jié)構(gòu):鄰接矩陣、鄰接表 排序算法
- 為什么學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu): 根據(jù)不同的應(yīng)用,靈活選擇最合適的數(shù)據(jù)結(jié)構(gòu)
數(shù)據(jù)結(jié)構(gòu) + 算法 = 程序
二、數(shù)組
1、數(shù)組基礎(chǔ)
- 用來(lái)存儲(chǔ)一組類(lèi)型相同的數(shù)據(jù)
- 在內(nèi)存中,分配連續(xù)的空間,數(shù)組創(chuàng)建時(shí)要指定容量(大?。?/li>
- 數(shù)據(jù)類(lèi)型[] 數(shù)組名 int[] arr = new int[10] int[] arr2 = {1,2,3,4}
- 索引---訪問(wèn)數(shù)組時(shí)通過(guò)索引進(jìn)行操作
- 索引從0開(kāi)始,最大為 arr.length -1
- 常見(jiàn)的錯(cuò)誤: NullPointException和ArrayIndexOutOfBoundsException
- 常見(jiàn)的數(shù)組: 字符串, 對(duì)象數(shù)組,哈希表
2.Java中數(shù)組的特點(diǎn)
(1)數(shù)組在內(nèi)存中連續(xù)分配;
(2)創(chuàng)建數(shù)組時(shí)要指明數(shù)組的大小;
(3)可以通過(guò)索引進(jìn)行訪問(wèn),索引從0開(kāi)始,這里索引可以理解為偏移量;
(4)使用索引:
- 獲取指定索引位置的值——arr[index]
- 修改指定索引位置的值——arr[inedx]
- 刪除數(shù)組中元素(假刪除)
(5)數(shù)組的遍歷:將數(shù)組中元素打印出來(lái);
(6)數(shù)組創(chuàng)建好之后,大小不能改變。
3.演示數(shù)組的使用
import java.util.Arrays;
import java.util.Comparator;
public class ArrayDome {
public static void main(String[] args) {
//練習(xí)數(shù)組的相關(guān)知識(shí)
//1.定義數(shù)組
int[] ints = new int[]{1,1,4,5,1,4};
//獲取數(shù)組長(zhǎng)度
int length = ints.length;
//獲取指定元素位置
int num = ints[2];
System.out.println(num);
//修改元素
ints[2] =100;
num = ints[2];
System.out.println(num);
//遍歷數(shù)組
for(int i= 0;i < ints.length;i++){
System.out.print(ints[i]+"\t");
}
System.out.println();
//數(shù)組越界錯(cuò)誤
try{
System.out.println(ints[length]);
}catch (Exception e){
e.printStackTrace();
}
//數(shù)組為空錯(cuò)誤
String[] strings = null;
try{
System.out.println(strings);
length = strings.length;
}catch (Exception e){
e.printStackTrace();
}
//數(shù)組的排序
Arrays.sort(ints);
for(int i= 0;i < ints.length;i++){
System.out.print(ints[i]+"\t");
}
System.out.println();
}
}
輸出結(jié)果:
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-803729.html
4.制作自己的數(shù)組
package leetcoke;
import java.util.Arrays;
import java.util.Comparator;
public class MyArray<T> {
private T[] data;
private int length;
private int size;
//構(gòu)造方法
public MyArray(int size) {
if (size <= 0) {
this.size = 16;
} else {
this.size = size;
}
this.length = 0;
this.data = (T[]) new Object[this.size];
}
//獲得當(dāng)前的數(shù)組容量
public int getSize() {
return this.size;
}
//判斷是否為空
public boolean isEmpty() {
return this.length == 0;
}
//添加
public void add(T data) throws IllegalAccessException {
add(this.length,data);
}
//在指定位置添加
public void add(int index, T data) throws IllegalAccessException {
if (index < 0 || index > this.length) {
throw new IllegalAccessException("你看你傳的是個(gè)啥!");
}
//判斷數(shù)組已滿(mǎn)
if(this.size==this.length){
//擴(kuò)容
resize(this.size*2);
}
for (int i = this.length; i > index; i--) {
this.data[i] = this.data[i - 1];
}
this.data[index] = data;
this.length++;
}
//擴(kuò)容
private void resize(int newCapacity){
T[] newData = (T[]) new Object[newCapacity];
newData = Arrays.copyOf(this.data,newCapacity);
this.data = newData;
this.size = newCapacity;
}
//修改指定位置的值
public void modifyValueByIndex(int index, T value) throws IllegalAccessException {
//入?yún)⑴袛? if (index < 0 || index >= this.size) {
throw new IllegalAccessException("你看你傳的是個(gè)啥!");
}
this.data[index] = value;
}
//獲取位置上的值
public T getValueByIndex(int index) throws IllegalAccessException {
//入?yún)⑴袛? if (index < 0 || index >= this.size) {
throw new IllegalAccessException("你看你傳的是個(gè)啥!");
}
return this.data[index];
}
//查詢(xún)
public int containsValue(T val) {
int i;
for (i = this.length - 1; i >= 0; i--) {
if (this.data[i] == val) {
return i;
}
}
return i;
}
//根據(jù)索引刪除
public T removeByIndex(int index) throws IllegalAccessException {
//入?yún)⑴袛? if (this.length == 0) throw new IllegalAccessException("已經(jīng)一點(diǎn)都不剩下了!");
if (index < 0 || index >= this.size) {
throw new IllegalAccessException("你看你傳的是個(gè)啥!");
}
T res = this.data[index];
for (int i = index + 1; i < this.length; i++) {
this.data[i - 1] = this.data[i];
}
this.length--;
if(this.length<=this.size/3 && this.size/2!=0){
resize(this.size/2);
}
return res;
}
@Override
public String toString() {
StringBuffer buffer = new StringBuffer("[");
for (int i = 0; i < this.length; i++) {
buffer.append(this.data[i]);
if (i != this.length - 1) {
buffer.append(",");
}
}
buffer.append("]");
return buffer.toString();
}
//排序
private T[] toArray1() {
return Arrays.copyOf(data, length);
}
public void sort() {
T[] array = toArray1();
Arrays.sort(array, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
if ((o1 instanceof Integer) || (o1 instanceof Float) || (o1 instanceof Double) || (o1 instanceof Short) || (o1 instanceof Long) || (o1 instanceof Byte)) {
return (Integer) o1 - (Integer) o2;
}
return 0;
}
});
for(int i = 0;i < this.length;i++){
this.data[i] = array[i];
}
}
public static void main(String[] args) throws IllegalAccessException {
MyArray<Integer> array = new MyArray<>(5);
//輸入數(shù)組的值
for (int i = 0; i < 5; i++) {
array.add((int) (Math.random() * 100 % 11));
}
try {
System.out.println(array.getSize());
array.add(3, 8);
} catch (Exception e) {
e.printStackTrace();
}
//排序
array.sort();
//遍歷
System.out.println(array);
//查詢(xún)
try {
System.out.println(array.getValueByIndex(2));
//找索引
System.out.println("8在數(shù)組中的位置為:" + array.containsValue(8));
} catch (Exception e) {
e.printStackTrace();
}
//刪除值
try {
System.out.println(array.removeByIndex(array.containsValue(8)));
} catch (Exception e) {
e.printStackTrace();
}
//刪除索引的位置
try {
System.out.println(array.removeByIndex(3));
} catch (Exception e) {
e.printStackTrace();
}
//向數(shù)組中指定位置添加元素
try {
System.out.println(array.getSize());
array.add(3, 8);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(array);
coke coke1 = new coke("coke", 18);
coke coke2 = new coke("coke", 20);
MyArray<coke> cokeMyArray = new MyArray<>(0);
cokeMyArray.add(coke1);
cokeMyArray.add(coke2);
System.out.println(cokeMyArray);
}
}
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-803729.html
到了這里,關(guān)于數(shù)據(jù)結(jié)構(gòu)Java版(1)——數(shù)組的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!