对象作为Java中的数组

Arrays are a group of homogeneous variables is a type of primitive data type which can be used in almost every programming language and referenced are by a common name. Though arrays in all programming languages may have initial properties as per the advancement of technologies and languages and their requirements arrays got more powerful and useful due to their less consumption and easy implementations. The arrays in Java functions differently than the ones we are introduced in C / C++:

数组是一组同构变量,是一种原始数据类型,几乎可以在每种编程语言中使用,并以通用名称引用。 尽管随着技术和语言的发展,所有编程语言中的数组都可能具有初始属性,并且由于其消耗较少且易于实现,所以数组数组的需求变得更加强大和有用。 Java中的数组功能与我们在C / C ++中引入的数组不同:

  • Java arrays are dynamically allocated as it involves two processes namely, the first one being the declaration of an array variable of desired variable type and the second process involves allocation of memory using the ‘new’ keyword and then assigning it to the array variable.

    Java数组是动态分配的,因为它涉及两个过程,第一个过程是声明所需变量类型的数组变量,第二个过程涉及使用'new'关键字分配内存,然后将其分配给数组变量。

  • Arrays in Java are nothing but objects as we can find the size of a using the member function length().

    Java中的数组不过是对象,因为我们可以使用成员函数length()找到a的大小。

  • A Java array variable like other variables can be declared with using '[]' after the data type of the array.

    像其他变量一样,可以在数组的数据类型之后使用'[]声明Java数组变量。

  • Each array in Java is ordered and starts from '0' index.

    Java中的每个数组都是有序的,并且从“ 0”索引开始。

  • A Java array can be used as a local variable or static field or even as method parameters.

    Java数组可用作局部变量或静态字段,甚至可用作方法参数。

  • There exists a superclass of arrays in Java and are known as Objects.

    Java中存在数组的超类,被称为对象。

  • The array cannot be a float, long, or short, it can only and only be an int value.

    该数组不能是float , long或short ,只能并且只能是int值。

As discussed above it is clear that in Java an array can store primitive data values as well as an object of classes. In case of primitive data values, the values are stored in contiguous memory locations whereas when arrays are used to store an object of classes then the values are stored into heap segments.

如上所述,很明显,在Java中,数组可以存储原始数据值以及类的对象。 如果是原始数据值,则将这些值存储在连续的内存位置中,而当使用数组存储类的对象时,则将这些值存储到堆段中。

Object array can be created just like normal arrays in Java.

可以像创建Java中的普通数组一样创建对象数组

Example

    //Where Student is a user-defined class
    Student[] Arr = new Student[7];  

In the above example, after the declaration of the array, there is a formation of 7 memory spaces each of sizes equal to the class Student.

在上面的示例中,在声明数组之后,形成了7个内存空间,每个内存空间的大小等于Student类。

Object as an Array in Java

用Java将对象演示为数组的程序 (Program to demonstrate object as an array in Java)

import java.util.Scanner;

class Employee
{
	private int code;
	private String name;
	private long salary;

	Scanner in = new Scanner(System.in);

	void getEmployee()
	{
		System.out.println("Enter Code: ");
		code=in.nextInt();
		System.out.println("Enter Name: ");
		name=in.next();
		System.out.println("Enter Salary: ");
		salary=in.nextLong();
	}

	void putEmployee()
	{
		System.out.println(code+","+name+","+salary);
	}
}

public class Program{
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);

		System.out.println("No. of Employees: ");
		int n=in.nextInt();
		Employee E[] = new Employee[n];

		for(int i=0;i<E.length;i++)
		{
			E[i]=new Employee();
			E[i].getEmployee();
		}

		for(int i=0;i<E.length;i++)
		{
			E[i].putEmployee();
		}
	}
}

Output

输出量

    No. of Employees:
    5
    Enter Code:
    1
    Enter Name:
    David
    Enter Salary:
    20000
    Enter Code:
    2
    Enter Name:
    James
    Enter Salary:
    15500
    Enter Code:
    3
    Enter Name:
    Peter
    Enter Salary:
    10000
    Enter Code:
    4
    Enter Name:
    Ryan
    Enter Salary:
    16000
    Enter Code:
    5
    Enter Name:
    Jonas
    Enter Salary:
    25000

    1,David,20000
    2,James,15500
    3,Peter,10000
    4,Ryan,16000
    5,Jonas,25000


翻译自: https://www.includehelp.com/java/object-as-an-array.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值