在 Java 中获取整形数组的 ArrayList 您所在的位置:网站首页 arraylist怎么取元素 在 Java 中获取整形数组的 ArrayList

在 Java 中获取整形数组的 ArrayList

2023-09-27 00:21| 来源: 网络整理| 查看: 265

创建整数类型的 ArrayList 将整数元素添加到 ArrayList 中的索引 通过索引访问 ArrayList 中的元素 整数数组的 ArrayList 从 ArrayList 访问整数数组元素 ArrayList 的 ArrayList 总结 在 Java 中获取整形数组的 ArrayList

本教程介绍了如何在 Java 中获取整数的 ArrayList,并列出了一些示例代码来理解该主题。

クラウド・コンピューテ...

Please enable JavaScript

クラウド・コンピューティングの歴史

ArrayList 是一个动态或可调整大小的数组。它是 Java 集合框架的一部分。ArrayList 用于克服普通数组大小固定的问题。在本教程中,我们将学习如何创建整数 ArrayList。

创建整数类型的 ArrayList

ArrayList 或任何其他集合不能存储原始数据类型,例如 int。如果我们编写如下所示的代码,那么我们将得到一个编译错误。

public class Demo { public static void main(String[] args) { ArrayList arrList; } } Ezoic

输出:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error, insert "Dimensions" to complete ReferenceType at snippet.Demo.main(Demo.java:7)

相反,我们使用包装类将基元存储在 ArrayList 中。每个原始数据类型都有一个对应的包装类,代表相同类型的对象。对于 int 数据类型,包装类称为 Integer。因此,要创建一个整数 ArrayList,我们需要使用 Integer 包装类作为其类型。

import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); } }

我们现在可以使用类的 add() 方法将整数添加到 ArrayList。

import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); arrList.add(5); arrList.add(7); arrList.add(12); } }

ArrayList,就像普通数组一样,遵循从零开始的索引。我们可以在 add() 方法中指定要添加对象的索引。出现在该索引处的元素及其右侧的所有元素将向右移动一位。

将整数元素添加到 ArrayList 中的索引 import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); arrList.add(5); arrList.add(7); arrList.add(12); arrList.add(1, 199);//Inserting 199 at index 1. } } 通过索引访问 ArrayList 中的元素

我们可以通过使用它们的索引来获取单个 ArrayList 项目。将索引值传递给 get() 方法以获取所需的元素。

import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); arrList.add(5); arrList.add(7); arrList.add(12); arrList.add(1, 199);//Inserting 199 at index 1. System.out.println("Element at index 1: " + arrList.get(1)); System.out.println("Element at index 2: " + arrList.get(2)); } } Ezoic

输出:

Element at index 1: 199 Element at index 2: 7

我们还可以使用单个打印语句打印整个 ArrayList。

Ezoic import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); arrList.add(5); arrList.add(7); arrList.add(12); System.out.println("Printing the ArrayList: " + arrList); arrList.add(1, 199);//Inserting 199 at index 1. System.out.println("Printing the ArrayList: " + arrList); } } Ezoic

输出:

Printing the ArrayList: [5, 7, 12] Printing the ArrayList: [5, 199, 7, 12] 整数数组的 ArrayList

我们可以创建一个 ArrayList,其中每个元素本身都是一个数组。我们使用数据类型和方括号来创建一个新数组。

类似地,我们使用 int[] 定义了 ArrayList 的类型。我们不能使用像 int 这样的基元作为 ArrayList 类型,但我们可以使用 int[]。这是因为 Java 中的数组是对象,而不是基元。并且 ArrayList 可以由任何对象类型(在我们的例子中为数组)创建。

ArrayList arrList = new ArrayList();

我们可以执行我们上面讨论的所有基本操作。我们需要使用 Arrays 的 toString() 方法将数组正确打印到控制台。

import java.util.ArrayList; import java.util.Arrays; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); int[] arr1 = {2, 4, 6}; int[] arr2 = {3, 6, 9}; int[] arr3 = {5, 10, 15}; //Adding int arrays to the ArrayList arrList.add(arr1); arrList.add(arr2); arrList.add(arr3); //Fetching the array from the List int[] arrAtIdx1 = arrList.get(1); //Printing the fetched array using the toString() method of Arrays System.out.println("The Second array in the List is: " + Arrays.toString(arrAtIdx1)); } } Ezoic

输出:

Ezoic The Second array in the List is: [3, 6, 9] 从 ArrayList 访问整数数组元素

我们还可以访问 ArrayList 中存在的 int 数组的各个元素。我们将使用数组索引来做到这一点。例如,如果我们希望访问第三个数组的第二个元素,那么我们将使用以下代码:

import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); int[] arr1 = {2, 4, 6}; int[] arr2 = {3, 6, 9}; int[] arr3 = {5, 10, 15}; //Adding int arrays to the ArrayList arrList.add(arr1); arrList.add(arr2); arrList.add(arr3); //Fetching the second element of the third array int[] thirdArr = arrList.get(2); int secondElement = thirdArr[1]; System.out.println("Second Element of the Third Array is: " + secondElement); } }

输出:

Second Element of the Third Array is: 10 Ezoic

但是,我们需要额外的代码来打印数组的整个 ArrayList。

import java.util.ArrayList; import java.util.Arrays; public class Demo { public static void main(String[] args) { ArrayList arrList = new ArrayList(); int[] arr1 = {2, 4, 6}; int[] arr2 = {3, 6, 9}; int[] arr3 = {5, 10, 15}; //Adding int arrays to the ArrayList arrList.add(arr1); arrList.add(arr2); arrList.add(arr3); for(int i = 0; i ArrayList present at index 1 is: [3, 6, 9] ArrayList at index 0 is [2, 4, 6] ArrayList at index 1 is [3, 6, 9] ArrayList at index 2 is [5, 10, 15]

如果你希望访问 ArrayList 的各个元素,请使用 get() 方法两次。例如,如果你想要第三个 ArrayList 的第二个元素,则使用:

import java.util.ArrayList; public class Demo { public static void main(String[] args) { ArrayList arrListOfarrLists = new ArrayList(); //Creating individual ArrayLists ArrayList arrList1 = new ArrayList(); arrList1.add(2); arrList1.add(4); arrList1.add(6); ArrayList arrList2 = new ArrayList(); arrList2.add(3); arrList2.add(6); arrList2.add(9); ArrayList arrList3 = new ArrayList(); arrList3.add(5); arrList3.add(10); arrList3.add(15); //Adding ArrayLists to the ArrayList arrListOfarrLists.add(arrList1); arrListOfarrLists.add(arrList2); arrListOfarrLists.add(arrList3); //Fetching second element of the third ArrayList ArrayList thirdList = arrListOfarrLists.get(2); int secondElement = thirdList.get(1); System.out.print("second element of the third ArrayList is: " + secondElement); } }

输出:

second element of the third ArrayList is: 10 总结

ArrayList 可能是 Java 中最常用的集合。它是一种简单的数据结构,用于存储相同类型的元素。我们不能创建像 int 这样的原始类型的 ArrayList。我们需要使用这些基元的包装类。ArrayList 类提供了从列表中添加和获取元素的便捷方法。我们还可以创建数组的 ArrayList 或 ArrayLists 的 ArrayList。它们主要用于以二维矩阵格式或表格格式表示数据。最好使用 ArrayLists 的 ArrayList,因为它不会限制其大小。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有