首页 > Java > java教程 > Java 中的动态数组

Java 中的动态数组

WBOY
发布: 2024-08-30 15:28:45
原创
1087 人浏览过

Java 中的动态数组意味着根据用户需求拉伸或缩小数组的大小。当从数组中删除一个元素时,数组大小必须缩小,如果向数组中添加一个元素,则数组大小会变长。数组用于存储同质元素,意味着一次可以存储相同类型的元素。

Java 中动态数组的声明

示例: 我们可以存储整数、浮点数、双精度数、字符串、字符、对象等,但一次只能存储任何特定类型。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

数组可以通过 3 种方式声明:

1。数组[]

示例:

int Array[]={1,2,4};
登录后复制

2。 [] 数组

示例:

int[] Array ={1,2,4};
登录后复制

3。 []A

示例:

int []Array ={1,2,4};
登录后复制

数组元素是如何迭代的?

数组元素使用以下方式迭代:

  • For 循环
  • While 循环
  • 对于每个循环

还有其他替代方法可以使数组变得动态吗?

  • 是的,通过使用Java集合,我们可以实现这一点。
  • 对于动态数组,我们可以使用ArrayList类。
  • 数组列表的大小可以根据用户操作自动增加或减少。

集合Arraylist相比普通动态数组有什么优势?

  • 根据用户需求,数组大小可以增加(拉伸)或减少(收缩),但首先在数组中,我们必须通过编写添加元素、删除元素等自定义代码,将普通数组变成动态数组。
  • Arraylist 内部实现仅涉及数组概念。

动态数组在 Java 中如何工作?

  • 要将普通数组变成动态数组,我们必须编写自定义逻辑,用于添加、删除元素、增加和减少大小和容量等

语法:

class DynamicArray
{
addingElements()
{
//custom logic
}
addingElementsAtIndex(int index, int element)
{
//custom logic
}
removingElements()
{
//custom logic
}
removingElementsAtIndex(int index, int element)
{
//custom logic
}
increasingSize()
{
//custom logic
}
decreasingSize()
{
//custom logic
}
printArrayElements()
{
//custom logic
}
.
.
.
}
登录后复制
  • ArrayList集合中,不需要编写自定义逻辑。它将提供添加、删除元素、获取大小和容量、根据索引获取元素、根据索引删除元素等所有自定义方法

语法:

class ArrayListLogic
{
List<Generic Type> list=new ArrayList<Generic Type>();
list.add();
list.remove(index,element);
.
.
.
}
登录后复制

Java 中的动态数组示例

下面给出了 Java 中动态数组的示例:

示例#1

将元素添加到数组并动态增加大小和容量。

代码:

package com.dynamicarray;
import java.util.Arrays;
public class DynamicArray {
// declaring an array
int myArray[];
// stores the present size of the array
int sizeOfMyArray;
// stores total capacity of an array
int arrayCapacity;
// initializing array, size and capacity
public DynamicArray() {
myArray = new int[2];
sizeOfMyArray = 0;
arrayCapacity = 2;
}
// method for adding elements
public void addElementsToArray(int element) {
// makes the capacity double if all the array elements filled
if (sizeOfMyArray == arrayCapacity) {
increaseCapacity(2);
}
myArray[sizeOfMyArray] = element;
sizeOfMyArray++;
}
// method for adding elements to specific position
public void addElementAtPosition(int position, int value) {
// makes the capacity double if all the array elements filled
if (sizeOfMyArray == arrayCapacity) {
increaseCapacity(2);
}
// shifting array elements
for (int p = sizeOfMyArray - 1; p >= position; p--) {
myArray[p + 1] = myArray[p];
}
// adding the element at specific position
myArray[position] = value;
sizeOfMyArray++;
}
// method for getting the element from specific position
public int getElementAtposition(int position) {
return myArray[position];
}
// method for increasing capacity if all the elements in an array filled
public void increaseCapacity(int minimumCapacity) {
int temp[] = new int[arrayCapacity * minimumCapacity];
for (int p = 0; p < arrayCapacity; p++) {
temp[p] = myArray[p];
}
myArray = temp;
arrayCapacity = arrayCapacity * minimumCapacity;
}
// method for array current size
public int displaySize() {
return sizeOfMyArray;
}
// method for array total capacity
public int displayCapacity() {
return arrayCapacity;
}
// method for display all elements
public void displayArrayElements() {
System.out.println("elements in array are :" + Arrays.toString(myArray));
}
public static void main(String[] args) {
DynamicArray array = new DynamicArray();
System.out.println("===================================================================");
System.out.println("Inital array size " + array.displaySize() + " and initial capacity " + array.displayCapacity());
System.out.println("===================================================================");
// adding elements at index 0 and 1
array.addElementsToArray(10);//line 1
array.addElementsToArray(20);//line 2
System.out.println("Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity());
array.addElementsToArray(30); //line 3
System.out.println("Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity());
array.displayArrayElements(); //line 4
// adding element at index 1
array.addElementAtPosition(1, 50);
System.out.println("Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity());
array.displayArrayElements();
// adding element at index 2
array.addElementAtPosition(2, 60);
System.out.println("Size of myArray =>" + array.displaySize() + " and Capacity :" + array.displayCapacity());
array.displayArrayElements();
}
}
登录后复制

输出:

Java 中的动态数组

说明:

  • 在第1行和第2行添加了2个元素;之后,我们尝试在第 3 行添加一个元素,但数组的初始容量只有 2。
  • 当我们尝试插入第三个元素时,数组容量增加到 4(因为我们指定容量=2*初始大小)。
  • 因此,我们也可以添加第 3 个rd 元素。
  • 第 4 行显示了所有数组元素。

示例#2

  • 从数组中删除元素并动态减小大小和容量。
  • 这个例子是上面例子的延续。

代码:

package com.dynamicarray;
import java.util.Arrays;
public class DynamicArray {
// declaring an array
int myArray[];
// stores the present size of the array
int sizeOfMyArray;
// stores total capacity of an array
int arrayCapacity;
// initializing array, size and capacity
public DynamicArray() {
myArray = new int[2];
sizeOfMyArray = 0;
arrayCapacity = 2;
}
// method for adding elements
public void addElementsToArray(int element) {
// makes the capacity double if all the array elements filled
if (sizeOfMyArray == arrayCapacity) {
increaseCapacity(2);
}
myArray[sizeOfMyArray] = element;
sizeOfMyArray++;
}
// method for adding elements to specific position
public void addElementAtPosition(int position, int value) {
// makes the capacity double if all the array elements filled
if (sizeOfMyArray == arrayCapacity) {
increaseCapacity(2);
}
// shifting array elements
for (int p = sizeOfMyArray - 1; p >= position; p--) {
myArray[p + 1] = myArray[p];
}
// adding the element at specific position
myArray[position] = value;
sizeOfMyArray++;
}
// method for getting the element from specific position
public int getElementAtposition(int position) {
return myArray[position];
}
// method for removing elements
public void removeAtPosition(int position) {
if (position >= sizeOfMyArray || position < 0) {
System.out.println("Opps!No elements found " + position + " position");
} else {
for (int p = position; p < sizeOfMyArray - 1; p++) {
myArray[p] = myArray[p + 1];
}
myArray[sizeOfMyArray - 1] = 0;
sizeOfMyArray--;
}
}
// method for increasing capacity if all the elements in an array filled
public void increaseCapacity(int minimumCapacity) {
int temp[] = new int[arrayCapacity * minimumCapacity];
for (int p = 0; p < arrayCapacity; p++) {
temp[p] = myArray[p];
}
myArray = temp;
arrayCapacity = arrayCapacity * minimumCapacity;
}
// method for make an array size to initial size
public void makeInitialSize() {
System.out.println("Making an array to initial size");
int temp[] = new int[sizeOfMyArray];
for (int q = 0; q < sizeOfMyArray; q++) {
temp[q] = myArray[q];
}
myArray = temp;
arrayCapacity = myArray.length;
}
// method for array current size
public int displaySize() {
return sizeOfMyArray;
}
// method for array total capacity
public int displayCapacity() {
return arrayCapacity;
}
// method for display all elements
public void displayArrayElements() {
System.out.println("elements in array are :" + Arrays.toString(myArray));
}
public static void main(String[] args) {
DynamicArray array = new DynamicArray();
System.out.println("===================================================================");
System.out.println("Inital array size " + array.sizeOfMyArray + " and initial capacity " + array.arrayCapacity);
System.out.println("===================================================================");
array.addElementsToArray(10);
array.addElementsToArray(20);
array.addElementsToArray(30);
array.addElementsToArray(40);
array.displayArrayElements();
array.removeAtPosition(2);
System.out.println("Size after Remove Operation=>" + array.displaySize() + " and Capacity :"
+ array.displayCapacity());
array.displayArrayElements();
array.removeAtPosition(2);
System.out.println("Size after Remove Operation=>" + array.displaySize() + " and Capacity :"
+ array.displayCapacity());
array.displayArrayElements();
array.removeAtPosition(1);
System.out.println("Size after Remove Operation=>" + array.displaySize() + " and Capacity :"
+ array.displayCapacity());
array.displayArrayElements();
array.removeAtPosition(2);
System.out.println("Size after Remove Operation =>" + array.displaySize() + " and Capacity :"
+ array.displayCapacity());
array.displayArrayElements();
array.removeAtPosition(1);
System.out.println("Size after Remove Operation =>" + array.displaySize() + " and Capacity :"
+ array.displayCapacity());
array.displayArrayElements();
// Make the array to initial size
array.makeInitialSize();
System.out.println(" After trimming Size of myArray =>" + array.displaySize() + " and Capacity :"
+ array.displayCapacity());
array.displayArrayElements();
array.addElementsToArray(-5);
System.out.println("After trimming Size of myArray =>" + array.displaySize() + " and Capacity :"
+ array.displayCapacity());
array.displayArrayElements();
array.addElementsToArray(-6);
System.out.println("After trimming Size of myArray =>" + array.displaySize() + " and Capacity :"
+ array.displayCapacity());
array.displayArrayElements();
}
}
登录后复制

输出:

Java 中的动态数组

示例 #3

使用 ArrayList 的动态数组。

代码:

package com.dynamicarray;
import java.util.ArrayList;
import java.util.List;
public class ArrayListDynamic {
public static void main(String[] args) {
List<Integer> list=new ArrayList<Integer>();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
System.out.println("Adding the elements ArrayList =>"+list);
System.out.println("Adding the elements ArrayList size =>"+list.size());
/*Array List capacity formula newCapacity = (oldCapacity * 3/2) + 1*/
list.add(4, 50);
System.out.println("After adding the element at specific index =>"+list+" and size "+list.size());
list.remove(4);
list.remove(3);
System.out.println("After removing the elements =>"+list+" and size "+list.size());
}
}
登录后复制

输出:

Java 中的动态数组

结论

在普通的动态数组中,实现开发者必须编写自定义逻辑,而在集合ArrayList中,所有预定义方法都可用,因此无需编写自定义逻辑。

以上是Java 中的动态数组的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板