代码优化: 它使代码得以优化,我们可以高效地检索或排序数据。
随机访问: 我们可以获得位于索引位置的任何数据。
大小限制: : 我们只能在数组中存储元素的固定大小。它不会在运行时增加其大小。为了解决这个问题,在Java中使用了收集框架,该框架会自动增长。
单维数组
多维数组
dataType[] arr;
(or)dataType []arr;
(or)dataType arr[];
arrayRefVar=new datatype[size];
//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 instantiationa[0]=10;
//initializationa[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing arrayfor(int i=0;i<a.length;i++)
//length is the property of arraySystem.out.println(a[i]);
}
}
1020704050
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 arrayfor(int i=0;i<a.length;i++)
//length is the property of arraySystem.out.println(a[i]);
}
}
33345
for(data_type variable:array){
//body of the loop
}
//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);
}
}
33345
//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 arraymin(a);
//passing array to method}
}
3
//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 parameterstatic 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}
}
10224466
//Java Program to return an array from the method
class TestReturnArray{
//creating method which returns an arraystatic int[] get(){
return new int[]{
10,30,50,90,60}
;
}
public static void main(String args[]){
//calling method which returns an arrayint arr[]=get();
//printing the values of an array
for(int i=0;i<arr.length;i++)
System.out.println(arr[i]);
}
}
1030509060
//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)50607080
dataType[][] arrayRefVar;
(or)dataType [][]arrayRefVar;
(or)dataType arrayRefVar[][];
(or)dataType []arrayRefVar[];
int[][] arr=new int[3][3];
//3 row and 3 column
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 Program to illustrate the use of multidimensional array
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D arrayint 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 32 4 54 4 5
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 Program to get the
class name of array in Java
class Testarray4{
public static void main(String args[]){
//declaration and initialization of arrayint arr[]={
4,4,5}
;
//getting the class name of Java arrayclass c=arr.getClass();
String name=c.getName();
//printing the class name of Java array System.out.println(name);
}
}
I
public static void arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
//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 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:33345Printing clone of the array:33345Are both equal?false
//Java Program to demonstrate the addition of two matrices in Java
class Testarray5{
public static void main(String args[]){
//creating two matricesint a[][]={
{
1,3,4}
,{
3,4,5}
}
;
int b[][]={
{
1,3,4}
,{
3,4,5}
}
;
//creating another matrix to store the sum of two matricesint c[][]=new int[2][3];
//adding and printing addition of 2 matricesfor(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 86 8 10
//Java Program to multiply two matrices
public class MatrixMultiplicationExample{
public static void main(String args[]){
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};
int c[][]=new int[3][3];
//3 rows and 3 columns
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];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
6 6 6
12 12 12
18 18 18
Java将一个数组的所有元素复制到另一个数组
Java查找数组中每个元素
Java向左旋转数组的元素
Java打印数组的重复元素
Java打印数组的元素
Java以相反的顺序打印数组的元素
Java打印出现在偶数位置的数组元素
Java打印出现在奇数位置的数组元素
Java打印数组中最大的元素
Java打印数组中的最小元素
Java打印数组中存在的元素数
Java打印数组中所有项的总和
Java向右旋转数组的元素
Java以升序对数组的元素进行排序
Java以降序对数组的元素进行排序
Java查找数组中的第3大数字
Java查找数组中的第2大数字
Java查找数组中最大的数字
Java查找数组中的第2个最小数字
Java查找数组中的最小数字
Java删除数组中的重复元素
Java从数组打印奇数和偶数
如何用Java对数组排序