{"id":729,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java \u6570\u7ec4","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/729.html","title":{"rendered":"Java \u6570\u7ec4"},"content":{"rendered":"
\n
<\/p>\n
\u4ee3\u7801\u4f18\u5316<\/strong>: \u5b83\u4f7f\u4ee3\u7801\u5f97\u4ee5\u4f18\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u9ad8\u6548\u5730\u68c0\u7d22\u6216\u6392\u5e8f\u6570\u636e\u3002<\/span> \u5927\u5c0f\u9650\u5236: <\/strong>: \u6211\u4eec\u53ea\u80fd\u5728\u6570\u7ec4\u4e2d\u5b58\u50a8\u5143\u7d20\u7684\u56fa\u5b9a\u5927\u5c0f\u3002\u5b83\u4e0d\u4f1a\u5728\u8fd0\u884c\u65f6\u589e\u52a0\u5176\u5927\u5c0f\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5728Java\u4e2d\u4f7f\u7528\u4e86\u6536\u96c6\u6846\u67b6\uff0c\u8be5\u6846\u67b6\u4f1a\u81ea\u52a8\u589e\u957f\u3002<\/span> <\/p>\n <\/p>\n \u5355\u7ef4\u6570\u7ec4<\/span>
\n \u968f\u673a\u8bbf\u95ee<\/strong>: \u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u4f4d\u4e8e\u7d22\u5f15\u4f4d\u7f6e\u7684\u4efb\u4f55\u6570\u636e\u3002<\/span> <\/p>\n\u7f3a\u70b9<\/h3>\n
\n java\u4e2d\u7684\u6570\u7ec4\u7c7b\u578b<\/h3>\n
\n \u591a\u7ef4\u6570\u7ec4<\/span> <\/p>\n
\n Java\u4e2d\u7684\u5355\u7ef4\u6570\u7ec4<\/h2>\n
dataType[] arr;
(or)dataType []arr;
(or)dataType arr[];
<\/span><\/code><\/pre>\n<\/p><\/div>\n arrayRefVar=new datatype[size];
<\/span><\/code><\/pre>\n<\/p><\/div>\n Java\u6570\u7ec4\u7684\u793a\u4f8b<\/h3>\n
\/\/Java Program to illustrate how to declare, instantiate, initialize
\/\/and traverse the Java array.
class <\/span>Testarray{
public <\/span>static void <\/span>main(String args[]){
int <\/span>a[]=new int[5];
\/\/declaration and instantiationa[0]=10;
\/\/initializationa[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
\/\/traversing arrayfor(int <\/span>i=0;i<a.length;i++)
\/\/length is the property of arraySystem.out.println<\/span>(a[i]);
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 1020704050
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Java\u6570\u7ec4\u7684\u58f0\u660e\uff0c\u5b9e\u4f8b\u5316\u548c\u521d\u59cb\u5316<\/h2>\n
int <\/span>a[]={33,3,4,5};
\/\/declaration, instantiation and initialization
<\/span><\/code><\/pre>\n<\/p><\/div>\n \/\/Java Program to illustrate the use of declaration, instantiation
\/\/and initialization of Java array in a single line
class <\/span>Testarray1{
public <\/span>static void <\/span>main(String args[]){
int <\/span>a[]={33,3,4,5};
\/\/declaration, instantiation and initialization
\/\/printing arrayfor(int <\/span>i=0;i<a.length;i++)
\/\/length is the property of arraySystem.out.println<\/span>(a[i]);
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 33345
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u7528\u4e8eJava\u6570\u7ec4\u7684For-each\u5faa\u73af<\/h2>\n
\n for-each\u5faa\u73af\u6765\u6253\u5370Java\u6570\u7ec4<\/strong>\u3002 Java for-each\u5faa\u73af\u9010\u4e00\u6253\u5370\u6570\u7ec4\u5143\u7d20\u3002\u5b83\u5728\u53d8\u91cf\u4e2d\u5305\u542b\u4e00\u4e2a\u6570\u7ec4\u5143\u7d20\uff0c\u7136\u540e\u6267\u884c\u5faa\u73af\u7684\u4e3b\u4f53\u3002\n <\/div>\n for(data_type variable:array){
\/\/body of the loop
} <\/span><\/code><\/pre>\n<\/p><\/div>\n \/\/Java Program to print <\/span>the array elements using for-each loop
class <\/span>Testarray1{
public <\/span>static void <\/span>main(String args[]){
int <\/span>arr[]={33,3,4,5};
\/\/printing array using for-each loop
for(int <\/span>i:arr)System.out.println<\/span>(i);
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 33345
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n\u5728Java\u4e2d\u5c06\u6570\u7ec4\u4f20\u9012\u7ed9\u65b9\u6cd5<\/h2>\n
\/\/Java Program to demonstrate the way of passing an array
\/\/to method.
class <\/span>Testarray2{
\/\/creating a method which receives an array as a parameter
static void <\/span>min(int <\/span>arr[]){
int <\/span>min=arr[0];
for(int <\/span>i=1;i<arr.length;i++) if<\/span>(min>arr[i]) min=arr[i];
System.out.println<\/span>(min);
}
public <\/span>static void <\/span>main(String args[]){
int <\/span>a[]={33,3,4,5};
\/\/declaring and initializing an arraymin(a);
\/\/passing array to method}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 3
<\/span><\/code><\/pre>\n<\/p><\/div>\n Java\u4e2d\u7684\u533f\u540d\u6570\u7ec4<\/h2>\n
\/\/Java Program to demonstrate the way of passing an anonymous array
\/\/to method.
public <\/span>class <\/span>TestAnonymousArray{
\/\/creating a method which receives an array as a parameterstatic void <\/span> printArray(int <\/span>arr[]){
for(int <\/span>i=0;i<arr.length;i++)System.out.println<\/span><\/span><\/span>(arr[i]);
}
public <\/span>static void <\/span>main(String args[]){
printArray(new int[]{10,22,44,66});
\/\/passing anonymous array to method}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 10224466
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u4ece\u65b9\u6cd5\u4e2d\u8fd4\u56de\u6570\u7ec4<\/h2>\n
\/\/Java Program to return <\/span>an array from the method
class <\/span>TestReturnArray{
\/\/creating method which returns an arraystatic <\/span>int[] get(){
return new <\/span>int[]{
10,30,50,90,60}
;
}
public <\/span>static void <\/span>main(String args[]){
\/\/calling method which returns an arrayint <\/span>arr[]=get();
\/\/printing the values of an array
for(int <\/span>i=0;i<arr.length;i++)
System.out.println<\/span><\/span><\/span>(arr[i]);
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 1030509060
<\/span><\/code><\/pre>\n<\/p><\/div>\n ArrayIndexOutOfBoundsException <\/h2>\n
\/\/Java Program to demonstrate the case <\/span>of
\/\/ArrayIndexOutOfBoundsException in a Java Array.
public <\/span>class <\/span>TestArrayException{
public <\/span>static void <\/span>main(String args[]){
int <\/span>arr[]={50,60,70,80};
for(int <\/span>i=0;i<=arr.length;i++){
System.out.println<\/span>(arr[i]);
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n Exception in thread \"main\"<\/span> java.lang.ArrayIndexOutOfBoundsException: 4 at TestArrayException.main<\/span>(TestArrayException.java:5)50607080
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Java\u4e2d\u7684\u591a\u7ef4\u6570\u7ec4<\/h2>\n
dataType[][] arrayRefVar;
(or)dataType [][]arrayRefVar;
(or)dataType arrayRefVar[][];
(or)dataType []arrayRefVar[];
<\/span><\/code><\/pre>\n<\/p><\/div>\n int[][] arr=new int[3][3];
\/\/3 row and 3 column
<\/span><\/code><\/pre>\n<\/p><\/div>\n 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;
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u591a\u7ef4Java\u6570\u7ec4\u7684\u793a\u4f8b<\/h3>\n
\/\/Java Program to illustrate the use of multidimensional array
class <\/span>Testarray3{
public <\/span>static void <\/span>main(String args[]){
\/\/declaring and initializing 2D arrayint <\/span>arr[][]={
{1,2,3},{2,4,5},{4,4,5}};
\/\/printing 2D array
for(int <\/span>i=0;i<3;i++){
for(int <\/span>j=0;j<3;j++){
System.out.print<\/span>(arr[i][j]+\" \"<\/span>);
}
System.out.println<\/span>();
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 1 2 32 4 54 4 5
<\/span><\/code><\/pre>\n<\/p><\/div>\n Java\u4e2d\u7684\u952f\u9f7f\u72b6\u6570\u7ec4<\/h2>\n
class TestJaggedArray{
public <\/span>static void <\/span>main(String[] args){
\/\/declaring a 2D array with odd columns
int arr[][] = new <\/span>int[3][];
arr[0] = new <\/span>int[3];
arr[1] = new <\/span>int[4];
arr[2] = new <\/span>int[2];
\/\/initializing a jagged array
int count = 0;
for <\/span>(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 <\/span>(int i=0; i<arr.length; i++){
for <\/span>(int j=0; j<arr[i].length; j++){
System.out.print<\/span>(arr[i][j]+\" \"<\/span>);
}
System.out.println<\/span><\/span>();\/\/new line
}
}
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n 0 1 2 3 4 5 6 7 8
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n Java\u6570\u7ec4\u7684\u7c7b\u540d\u662f\u4ec0\u4e48\uff1f<\/h2>\n