个性化阅读
专注于IT技术分析

Java数组

本文概要

通常情况下,一个阵列是类似类型,其具有连续的存储器位置的元素的集合。

Java数组是包含类似的数据类型的元素的对象。此外,阵列的元素被存储在连续的存储位置。这是我们存储相似元素的数据结构。我们只能保存一组固定的一个Java数组中的元素。

阵列中的Java是基于索引的,所述阵列的所述第一元素存储第0个索引处,第二元件被存储在第一索引等等。

与C / C ++,我们可以得到使用长度构件阵列的长度。在C / C ++,我们需要使用sizeof操作符。

在Java中,阵列是动态生成的类的对象。 Java数组继承对象类,并实现了Serializable以及Cloneable的接口。我们可以存储在Java中的数组的原始值或对象。像C / C ++,我们也可以在Java中创建一个维或多维数组。

此外,Java提供这是不是在C / C ++提供匿名阵列的特征。

优点

  • 代码优化:它使代码的优化,我们可以检索或排序有效的数据。
  • 随机访问:我们可以得到位于索引位置的任何数据。

缺点

  • 大小限制:我们只能存储数组中元素的固定大小。它在运行时不扩大其规模。为了解决这个问题,集合框架在Java中使用自动增长。

在Java数组类型

有两种类型的阵列。

  • 一维数组
  • 多维数组

在Java中一维数组

语法来声明Java中的数组

dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];

Java中的数组的实例化

arrayRefVar=new datatype[size];

Java数组的例子

让我们来看看Java数组,在这里我们要声明,实例化,初始化和遍历数组的简单例子。

//Java Program to illustrate how to declare,instantiate,initialize
//and traverse the Java array.
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}

输出:

10
20
70
40
50

声明,实例和初始化Java数组

我们可以声明,实例化和Java数组的初始化在一起:

int a[]={33,3,4,5};//declaration,instantiation and initialization

让我们来看看简单的例子来打印此阵。

//Java Program to illustrate the use of declaration,instantiation 
//and initialization of Java array in a single line
class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5};//declaration,instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}

输出:

33
3
4
5

for-each循环的Java数组

我们还可以打印使用for-each循环的Java数组。在Java for-each循环打印阵列元件一个接一个。它保持在可变的阵列元素,那么执行循环体。

的for-each循环的语法给出如下:

for(data_type variable:array){
//body of the loop
}

让我们来看看的例子使用for-each循环打印Java数组的元素。

//Java Program to print the array elements using for-each loop
class Testarray1{
public static void main(String args[]){
int arr[]={33,3,4,5};
//printing array using for-each loop
for(int i:arr)
System.out.println(i);
}}

输出:

33
3
4
5

传球达阵的方法在Java中

我们可以通过在Java阵列的方法,使我们可以重新使用任何阵列上的相同的逻辑。

让我们来看看简单的例子来获得使用方法数组的最小数量。

//Java Program to demonstrate the way of passing an array
//to method.
class Testarray2{
//creating a method which receives an array as a parameter
static void min(int arr[]){
int min=arr[0];
for(int i=1;i<arr.length;i++)
 if(min>arr[i])
  min=arr[i];

System.out.println(min);
}

public static void main(String args[]){
int a[]={33,3,4,5};//declaring and initializing an array
min(a);//passing array to method
}}

输出:

3

匿名数组在Java中

Java支持匿名阵列的功能,让你不必同时传递数组到方法声明数组。

//Java Program to demonstrate the way of passing an anonymous array
//to method.
public class TestAnonymousArray{
//creating a method which receives an array as a parameter
static void printArray(int arr[]){
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}

public static void main(String args[]){
printArray(new int[]{10,22,44,66});//passing anonymous array to method
}}

输出:

10
22
44
66

从方法返回数组

我们还可以从Java中的方法返回一个数组。

//Java Program to return an array from the method
class TestReturnArray{
//creating method which returns an array
static int[] get(){
return new int[]{10,30,50,90,60};
}

public static void main(String args[]){
//calling method which returns an array
int arr[]=get();
//printing the values of an array
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}}

输出:

10
30
50
90
60

ArrayIndexOutOfBoundsException异常

Java虚拟机(JVM)抛出如果在负阵列的长度,等于阵列尺寸或大于所述阵列的尺寸,同时遍历阵列一个ArrayIndexOutOfBoundsException。

//Java Program to demonstrate the case of 
//ArrayIndexOutOfBoundsException in a Java Array.
public class TestArrayException{
public static void main(String args[]){
int arr[]={50,60,70,80};
for(int i=0;i<=arr.length;i++){
System.out.println(arr[i]);
}
}}

输出:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at TestArrayException.main(TestArrayException.java:5)
50
60
70
80

在Java中多维数组

在这种情况下,数据被存储在基于行和列索引(也称为矩阵形式)。

语法来声明在Java中多维数组

dataType[][] arrayRefVar; (or)
dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];

实施例实例在Java中多维数组

int[][] arr=new int[3][3];//3 row and 3 column

实施例初始化在Java中多维数组

arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;

多维Java数组的实施例

让我们来看看简单的例子来声明,实例化,初始化和打印2维数组。

//Java Program to illustrate the use of multidimensional array
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
 for(int j=0;j<3;j++){
   System.out.print(arr[i][j]+" ");
 }
 System.out.println();
}
}}

输出:

1 2 3
2 4 5
4 4 5

在Java中铁血阵列

如果我们在2D阵列创建奇数列的,它被称为锯齿形阵列。换句话说,它是与不同的列数的数组的数组。

//Java Program to illustrate the jagged array
class TestJaggedArray{
    public static void main(String[] args){
        //declaring a 2D array with odd columns
        int arr[][] = new int[3][];
        arr[0] = new int[3];
        arr[1] = new int[4];
        arr[2] = new int[2];
        //initializing a jagged array
        int count = 0;
        for (int i=0; i<arr.length; i++)
            for(int j=0; j<arr[i].length; j++)
                arr[i][j] = count++;
 
        //printing the data of a jagged array 
        for (int i=0; i<arr.length; i++){
            for (int j=0; j<arr[i].length; j++){
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();//new line
        }
    }
}

输出:

0 1 2 
3 4 5 6 
7 8

什么是Java数组的类名?

在Java中,一个阵列是一个对象。对于阵列对象,代理类被创建,其名称可通过的getClass()获得。在对象上getName()方法。

//Java Program to get the class name of array in Java
class Testarray4{
public static void main(String args[]){
//declaration and initialization of array
int arr[]={4,4,5};
//getting the class name of Java array
Class c=arr.getClass();
String name=c.getName();
//printing the class name of Java array 
System.out.println(name);

}}

输出:

I

复制Java数组

我们可以通过系统类的arraycopy()方法复制的阵列到另一个。

arraycopy方法的语法

public static void arraycopy(
Object src,int srcPos,Object dest,int destPos,int length
)

复制Java中的数组的例子

//Java Program to copy a source array into a destination array in Java
class TestArrayCopyDemo {
    public static void main(String[] args) {
        //declaring a source array
        char[] copyFrom = { 'd','e','c','a','f','f','e','i','n','a','t','e','d' };
        //declaring a destination array
        char[] copyTo = new char[7];
        //copying array using System.arraycopy() method
        System.arraycopy(copyFrom,2,copyTo,0,7);
        //printing the destination array
        System.out.println(String.valueOf(copyTo));
    }
}

输出:

caffein

克隆Java中的数组

因为,Java数组实现了Cloneable接口,我们可以创建Java数组的克隆。如果我们创建一个一维数组的克隆,它创建Java数组的深层副本。这意味着,它将复制实际值。但是,如果我们创建了一个多维数组的克隆,它创建Java数组,这意味着它复制引用的浅拷贝。

//Java Program to clone the array
class Testarray1{
public static void main(String args[]){
int arr[]={33,3,4,5};
System.out.println("Printing original array:");
for(int i:arr)
System.out.println(i);

System.out.println("Printing clone of the array:");
int carr[]=arr.clone();
for(int i:carr)
System.out.println(i);

System.out.println("Are both equal?");
System.out.println(arr==carr);

}}

输出:

Printing original array:
33
3
4
5
Printing clone of the array:
33
3
4
5
Are both equal?
false

2点矩阵在Java中的加

让我们看一个简单的例子,增加两个矩阵。

//Java Program to demonstrate the addition of two matrices in Java
class Testarray5{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};

//creating another matrix to store the sum of two matrices
int c[][]=new int[2][3];

//adding and printing addition of 2 matrices
for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println();//new line
}

}}

输出:

2 6 8
6 8 10

2点矩阵在Java中的乘法

在矩阵乘法的情况下,所述第一矩阵的一列元件通过其可通过以下给出的图像可以理解所述第二矩阵的所有列相乘。

让我们看一个简单的例子来的3行3列将两个矩阵相乘。

//Java Program to multiply two matrices
public class MatrixMultiplicationExample{
public static void main(String args[]){
//creating two matrices  
int a[][]={{1,1,1},{2,2,2},{3,3,3}};  
int b[][]={{1,1,1},{2,2,2},{3,3,3}};  
  
//creating another matrix to store the multiplication of two matrices  
int c[][]=new int[3][3];  //3 rows and 3 columns
  
//multiplying and printing multiplication of 2 matrices  
for(int i=0;i<3;i++){  
for(int j=0;j<3;j++){  
c[i][j]=0;    
for(int k=0;k<3;k++)    
{    
c[i][j]+=a[i][k]*b[k][j];    
}//end of k loop
System.out.print(c[i][j]+" ");  //printing matrix element
}//end of j loop
System.out.println();//new line  
}  
}}

输出:

6 6 6 
12 12 12 
18 18 18
赞(0)
未经允许不得转载:srcmini » Java数组

评论 抢沙发

评论前必须登录!