Java中的自定义ArrayList

在继续之前,让我们快速修改 阵列 ArrayList 迅速地所以在java中,我们看到数组是线性数据结构,提供了在内存地址空间中连续添加元素的功能,而ArrayList是属于集合框架的一个类。作为一名优秀的程序员,我们已经知道在数组之上使用ArrayList,尽管我们知道这两者之间的区别。现在,即使使用ArrayList,也有一个功能可以传递应该存储在ArrayList中的元素的数据类型,可以是对象、字符串、整数、双精度、浮点等。

null

语法:

Arraylist<DataType> al = new ArrayList<Datatype> ;

注: Java中的ArrayList(相当于C++中的vector)具有动态大小。它可以根据大小缩小或扩大。ArrayList是集合框架的一部分,存在于 JAVAutil包 .

语法:

ArrayList <E> list = new ArrayList <> ();

这里最重要的一点是,E在这里代表一个对象数据类型 整数 在这里Integer类包装了一个基元类型的值 智力 在一个物体上。Integer类型的对象包含一个类型为int的字段 包装类 在java中,因为它将在后端提供服务,如果我们清楚地了解 自动装箱和拆箱概念 这是因为在对列表中的元素执行操作时,它们的语法会有所不同,因此,如果考虑到将元素添加到自定义数组列表中的情况,并注意它们之间的语法差异,那么对概念的理解会耗尽。

语法:

ArrayList<Integer> al = new Arraylist<Integer>() ;al.add(1) ;

语法:

ArrayList alobj = new Arraylist() ;alobj(new Integer(1)) ;

让我们通过一个示例来理解以下内容:

插图:

图片[1]-Java中的自定义ArrayList-yiteyi-C++库

在这里,我们使用的是同一类型的所有元素,通常我们会使用这些元素。现在,让我们提出相同的图表流ArrayList,它只是以图中所示的方式支持多个数据。

图片[2]-Java中的自定义ArrayList-yiteyi-C++库

在上面的ArrayList中,我们可以清楚地看到存储在中的元素是不同类型的。因此,它确实引发了 限制。对于单一类型,不仅此列表do go使我们能够根据我们的类型灵活地创建列表,我们可以访问ArrayList中的数据类型。该列表在java中称为自定义ArrayList . 自定义ArrayList具有基于用户需求的属性,可以包含多种类型的数据。这些数据由一个定制的内部类提供,该类由各种基本对象数据类型的组合构成。

实施: 考虑一个例子,当我们必须把输入作为 N 学生人数及详情如下:

  • 卷号
  • 名称
  • 标志
  • 电话号码

假设我们不知道java中自定义Arraylist的概念,那么我们将创建下面列出的单个Arraylist。我们定义了4个ArrayList,并在每个ArrayList中相应地保存数据。

ArrayList<Integer> roll = new ArrayList<>();  // roll number
 ArrayList<String> name = new ArrayList<>(); // name
ArrayList<Integer> marks = new ArrayList<>(); //  marks
ArrayList<Long> phone = new ArrayList<>();  // phone number 

现在,我们将对每一个进行迭代,以获取学生数据,从而在更大程度上增加我们程序的时间复杂性,如下所示。

for (int i = 0; i < n; i++) {    // Adding all the values to each arraylist    // each arraylist has primitive datatypes        roll.add(rollnum_i);    name.add(name_i);    marks.add(marks_i);    phone.add(phone_i);}

现在,让我们通过实现上述概念来实现同样的目标。因此,为了构建我们的自定义ArrayList,请执行以下列出的步骤:

程序: 构建自定义ArrayList如下所示:

  1. 构建ArrayList对象并将其类型作为类数据放置。
  2. 定义一个类并将所需的实体放入构造函数中。
  3. 将这些实体链接到全局变量。
  4. 从ArrayList接收的数据属于存储多个数据的类类型。

实例

JAVA

// Java program to illustrate Custom ArrayList
// Importing ArrayList class from java.util package
import java.util.ArrayList;
// Class 1
// Outer class
// Main class
// CustomArrayList
class GFG {
// Custom class which has data type class has
// defined the type of data ArrayList
// size of input 4
int n = 4 ;
// Class 2
// Inner class
// The custom datatype class
class Data {
// Global variables of the class
int roll;
String name;
int marks;
long phone;
// Constructor has type of data that is required
Data( int roll, String name, int marks, long phone)
{
// Initialize the input variable from main
// function to the global variable of the class
// this keyword refers to current instance
this .roll = roll;
this .name = name;
this .marks = marks;
this .phone = phone;
}
}
// Method 1
// Main driver method
public static void main(String args[])
{
// Custom input data
int roll[] = { 1 , 2 , 3 , 4 };
String name[]
= { "Shubham" , "Atul" , "Ayush" , "Rupesh" };
int marks[] = { 100 , 99 , 93 , 94 };
long phone[] = { 8762357381L, 8762357382L,
8762357383L, 8762357384L };
// Creating an object of the class
GFG custom = new GFG();
// Now calling function to add the values to the
// arraylist
custom.addValues(roll, name, marks, phone);
}
public void addValues( int roll[], String name[],
int marks[], long phone[])
{
// local custom arraylist of data type
// Data having (int, String, int, long) type
// from the class
ArrayList<Data> list = new ArrayList<>();
for ( int i = 0 ; i < n; i++) {
// create an object and send values to the
// constructor to be saved in the Data class
list.add( new Data(roll[i], name[i], marks[i],
phone[i]));
}
// after adding values printing the values to test
// the custom arraylist
printValues(list);
}
// Method 2
// To print the values
public void printValues(ArrayList<Data> list)
{
// list- the custom arraylist is sent from
// previous function
for ( int i = 0 ; i < n; i++) {
// Data received from arraylist is of Data type
// which is custom (int, String, int, long)
// based on class Data
Data data = list.get(i);
// Print and display custom ArrayList elements
// that holds for student attribute
// Data variable of type Data has four primitive
// datatypes roll -int name- String marks- int
// phone- long
System.out.println(data.roll + " " + data.name
+ " " + data.marks + " "
+ data.phone);
}
}
}


输出

1 Shubham 100 87623573812 Atul 99 87623573823 Ayush 93 87623573834 Rupesh 94 8762357384

本文由 Shubham Saxena .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享