集合ArrayList底層結構使用的是數組,主要實現的方法有增刪改查,其中核心的部分是當增加一個元素時候的數組擴容處理。不多說,直接上碼:
/** * ArrayList底层使用数组结构,暴露方法,增删改查 其中元素增加,需要判断数组的长度 */ public class MyArrayList<E> { private Object[] array;// 存放元素的数组 private Object[] EMPTY_ARRAY = {};// 空数组 private static final int DEFAULT_CAPACITY = 8;// 默认数组长度 private int size;// 数组中元素的个数 private int modSize;// 线性表修改的次数 public MyArrayList() { array = EMPTY_ARRAY;// 默认是给个空数组还是给个8个空间的数组呢? } public MyArrayList(int initCapacity) { if (initCapacity < 0) {// 传入的数量为负数,抛出异常 throw new IllegalArgumentException("参数错误:" + initCapacity); } else if (initCapacity == 0) {// 空数组 array = EMPTY_ARRAY; } else { array = new Object[initCapacity]; } } public MyArrayList(Collection<E> c) { Object[] obj = c.toArray(); if ((size = obj.length) != 0) {// 将Collection中的数据拷贝出来,防止污染 System.arraycopy(obj, 0, array, 0, size); } else { array = EMPTY_ARRAY; } } /** * 添加一个元素到线性表尾部->先判断数组大小和元素个数是否相同-》相同的话,需要扩容 * * @param e * @return */ public boolean add(E e) { array = judgeIsGrow(); array[size] = e; size++; modSize++; return true; } // 判断是否扩容 private Object[] judgeIsGrow() { if (size == array.length) { // 确定新数组的大小--》new出来--》将原来数组的数据拷贝到新数组中 int newSize = 0; if (size == 0) { newSize = DEFAULT_CAPACITY; } else { newSize = size < Integer.MAX_VALUE / 2 ? size << 1 : Integer.MAX_VALUE; } Object[] newArray = new Object[newSize]; System.arraycopy(array, 0, newArray, 0, size); array = newArray; } return array; } /** * 在第index位置插入一个元素 * * @param index * @param e * @return */ public boolean add(int index, E e) { checkArgument(index); array = judgeIsGrow(); Object[] newObj = new Object[array.length]; // 将array数组中的元素拷贝到新数组中 System.arraycopy(array, 0, newObj, 0, index); System.arraycopy(array, index, newObj, index + 1, size- index); newObj[index] = e; array = newObj; size++; modSize++; return true; } // 参数检查 private void checkArgument(int index) { if (index < 0) { throw new IllegalArgumentException("参数错误:" + index); } else if (index > size) { throw new IllegalArgumentException("超过数组长度,参数错误:" + index); } } /** * 删除第index个元素 * * @param index * @return */ @SuppressWarnings("unchecked") public E remove(int index) { Object[] newObj = new Object[array.length]; E obj = (E) array[index]; // 将array数组中的元素拷贝到新数组中 System.arraycopy(array, 0, newObj, 0, index); System.arraycopy(array, index + 1, newObj, index, size - index - 1); array = newObj; size--; modSize++; return obj; } @SuppressWarnings("unchecked") public E get(int index) { return (E) array[index]; } public void set(int index, E e) { array[index] = e; } public int size() { return size; } public static void main(String[] args) { MyArrayList<String> list = new MyArrayList<>(); for (int i = 0; i < 10; i++) { list.add("data "+i); } list.add(2,"add23"); list.add(11,"dddd"); list.remove(3); list.set(9, "dajdfljfl"); int size = list.size(); for (int i = 0; i < size; i++) { System.out.println("元素:"+(i+1)+" == " + list.get(i)); } } }
最後列印結果為:
元素:1 == data 0
元素:2 == data 1
元素:3 ====
元素:2 == data 1元素:3 ==== 2343 == == data 3元素:5 == data 4元素:6 == data 5元素:7 == data 6元素:8 == data 7元素:9 元素:8 == data 7元素:10 == dajdfljfl元素:11 == dddd

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver CS6
視覺化網頁開發工具