Java 数组列表在索引处插入/替换指南

479 阅读1分钟

Java 阵列列表在索引处插入/替换

在java中插入或替换指定索引的元素的快速指南。

1.概述

在本教程中,我们将学习如何在java的ArrayList中插入或替换一个指定索引的元素

使用ArrayList.add(int index, Object value) 方法在ArrayList的特定索引处添加任何对象或元素,使用ArrayList.set(int index, E value)在java中替换ArrayList特定索引处的

让我们来探讨一下这些例子

本文展示的所有例子都在GitHub上,文章最后给出了链接。

2. 例子 - 如何在Java中在ArrayList的特定索引处插入一个值?

ArrayList.add(int index, E element) 方法获取数组列表中要插入的新值的索引和要添加到ArrayList现有值中的新值的E元素。

add(index, value) 方法用于在ArrayList的给定索引位置插入给定元素。

让我们看看下面的例子,如何在ArrayList上调用add(),在索引3处添加新的值。

例子1

package com.javaprogramto.java8.arraylist.insert;

import java.util.ArrayList;
import java.util.List;

public class ArrayListInsertAddExample {

	public static void main(String[] args) {

		List<Integer> list1 = new ArrayList<>();

		list1.add(10);
		list1.add(20);
		list1.add(30);
		list1.add(40);
		list1.add(50);
		list1.add(60);

		System.out.println("List values before insertion - " + list1);

		list1.add(3, 333);

		System.out.println("List values after inserting the value 333 at index 3 - " + list1);
	}
}

输出:

List values before insertion - [10, 20, 30, 40, 50, 60]
List values after inserting the value 333 at index 3 - [10, 20, 30, 333, 40, 50, 60]

从输出中,我们可以看到一个新的值333被添加到索引3处,并且索引3的值右移了一个索引。同时,大小也增加了1。

例二

System.out.println("List values before insertion - " + list1);
System.out.println("list1 size before - "+list1.size());

list1.add(3, 333);

System.out.println("List values after inserting the value 333 at index 3 - " + list1);
System.out.println("list1 size after - "+list1.size());

输出

List values before insertion - [10, 20, 30, 40, 50, 60]
list1 size before - 6
List values after inserting the value 333 at index 3 - [10, 20, 30, 333, 40, 50, 60]
list1 size after - 7

另一个例子 3

下面的代码是关于字符串列表的

package com.javaprogramto.java8.arraylist.insert;

import java.util.ArrayList;
import java.util.List;

public class ArrayListInsertAddExample2 {

	public static void main(String[] args) {

		List<String> list2 = new ArrayList<>();

		list2.add("one");
		list2.add("two");
		list2.add("three");
		list2.add("four");
		list2.add("five");
		list2.add("six");

		System.out.println("List2 values before insertion - " + list2);
		System.out.println("list2 size before - "+list2.size());

		list2.add(3, "new four");

		System.out.println("List2 values after inserting the value 333 at index 3 - " + list2);
		System.out.println("list2 size after - "+list2.size());
	}
}

输出

List2 values before insertion - [one, two, three, four, five, six]
list2 size before - 6
List2 values after inserting the value 333 at index 3 - [one, two, three, new four, four, five, six]
list2 size after - 7

3. 例子 - 如何在Java中替换或更新ArrayList中特定索引的值?

使用set(int index, E element)来更新或用ArrayList的新值替换现有的值, 并输入索引和新值。

例四

package com.javaprogramto.java8.arraylist.insert;

import java.util.ArrayList;
import java.util.List;

public class ArrayListUpdateSetExample3 {

	public static void main(String[] args) {

		List<Integer> list1 = new ArrayList<>();

		list1.add(10);
		list1.add(20);
		list1.add(30);
		list1.add(40);
		list1.add(50);
		list1.add(60);

		System.out.println("List1 values before update - " + list1);
		System.out.println("list1 size before - "+list1.size());

		list1.set(3, 333);

		System.out.println("List1 values after updating index 3 value with new value 333 - " + list1);
		System.out.println("list1 size after - "+list1.size());
	}
}

输出

List1 values before update - [10, 20, 30, 40, 50, 60]
list1 size before - 6
List1 values after updating index 3 value with new value 333 - [10, 20, 30, 333, 50, 60]
list1 size after - 6

从输出中,我们可以看到索引3的值40被从列表中移除,并被更新为新的值333。

在替换了索引3的值之后,列表的大小没有变化。所以,列表的大小没有被修改,保持了原来的列表。

我们可以对任何类型的对象使用set(),比如封装类、字符串或任何用户定义的自定义对象。

4.迭代时更新列表的值的例子

在 set()方法的帮助下,我们可以在迭代过程中根据条件更新数组列表的值。

例五

在下面的例子中,如果数字能被5整除,那么就将其值更新为-1,否则就不替换该值。

package com.javaprogramto.java8.arraylist.insert;

import java.util.ArrayList;
import java.util.List;

public class ArrayListUpdateSetIterateExample4 {

	public static void main(String[] args) {

		List<Integer> list1 = new ArrayList<>();

		list1.add(4);
		list1.add(10);
		list1.add(24);
		list1.add(25);
		list1.add(5);
		list1.add(28);

		System.out.println("List1 values before update - " + list1);
		System.out.println("list1 size before - " + list1.size());

		for (int index = 0; index < list1.size(); index++) {
			if (list1.get(index) % 5 == 0) {
				list1.set(index, -1);
			}
		}

		System.out.println("List1 values after updating index 3 value with new value 333 - " + list1);
		System.out.println("list1 size after - " + list1.size());
	}
}

输出

List1 values before update - [4, 10, 24, 25, 5, 28]
list1 size before - 6
List1 values after updating index 3 value with new value 333 - [4, -1, 24, -1, -1, 28]
list1 size after - 6

列表中所有能被5整除的值都被更新为-1,列表大小不变。

5.总结

在这篇文章中,我们已经看到了如何在java中更新或插入ArrayList的特定索引的新值