Java Learning----Reference passing of arrays

Source: Internet
Author: User
Tags array sort

1.

public class arrayrefdemo01{
public static void Main (String args[]) {
int temp[] = {1,3,5};//define an array with static initialization
Fun (temp);//pass Array
for (int i=0;i<temp.length;i++) {
System.out.print (Temp[i] + ",");
}
}
public static void Fun (int x[]) {//Receive reference to an array of integral types
X[0] = 6;//Modify the first element
}
};

Operation Result:

6, 3, 5,

2.

public class arrayrefdemo02{
public static void Main (String args[]) {
int temp[] = fun ();//Instantiate an array by means of a method
Print (temp);//Printing array contents
}
public static void print (int x[]) {
for (int i=0;i<x.length;i++) {
System.out.print (X[i] + ",");
}
}
public static int[] Fun () {//returns an array
int ss[] = {1,3,5,7,9};//define an array
return SS;
}
};

Operation Result:

1,3,5,7,9

3.

public class arrayrefdemo03{
public static void Main (String args[]) {
int score[] = {67,89,87,69,90,100,75,90};//define an integral type array
int age[] = {31,30,18,17,8,9,1,39};//define an integral type array
Sort (score);//Array sort
Print (score);//Array printing
System.out.println ("\ n---------------------------");
Sort (age);//Array sort
print (age);//Array printing
}
public static void sort (int temp[]) {//Perform sort operation
for (int i=1;i<temp.length;i++) {
for (int j=0;j<temp.length;j++) {
if (Temp[i]<temp[j]) {
int x = Temp[i];
Temp[i] = Temp[j];
TEMP[J] = x;
}
}
}
}
public static void print (int temp[]) {//output array contents
for (int i=0;i<temp.length;i++) {
System.out.print (Temp[i] + "\ t");
}
}
};

Operation Result:

67 69 75 87 89 90 90 100
---------------------------
1891718303139

4.

public class arrayrefdemo04{
public static void Main (String args[]) {
int score[] = {67,89,87,69,90,100,75,90};//define an integral type array
int age[] = {31,30,18,17,8,9,1,39};//define an integral type array
Java.util.Arrays.sort (score);//array sorting
Print (score);//Array printing
System.out.println ("\ n---------------------------");
Java.util.Arrays.sort (age);//array sorting
print (age);//Array printing
}
public static void print (int temp[]) {//output array contents
for (int i=0;i<temp.length;i++) {
System.out.print (Temp[i] + "\ t");
}
}
};

Operation Result:

67 69 75 87 89 90 90 100
---------------------------
1891718303139

5.

public class arrayrefdemo05{
public static void Main (String args[]) {
int i1[] = {1,2,3,4,5,6,7,8,9};//source array
int i2[] = {11,22,33,44,55,66,77,88,99};//target array
Copy (i1,3,i2,1,3);//Call copy method
Print (I2);
}
Source array name, source array start point, target array name, target array start point, copy length
public static void copy (int s[],int s1,int o[],int s2,int len) {
for (int i=0;i<len;i++) {
O[s2+i] = S[s1+i];//copy operation
}
}
public static void print (int temp[]) {//output array contents
for (int i=0;i<temp.length;i++) {
System.out.print (Temp[i] + "\ t");
}
}
};

Operation Result:

4 5 6

6.

public class arrayrefdemo06{
public static void Main (String args[]) {
int i1[] = {1,2,3,4,5,6,7,8,9};//source array
int i2[] = {11,22,33,44,55,66,77,88,99};//target array
System.arraycopy (i1,3,i2,1,3);//Call the Copy method supported by the array in Java
Print (I2);
}
public static void print (int temp[]) {//output array contents
for (int i=0;i<temp.length;i++) {
System.out.print (Temp[i] + "\ t");
}
}
};

Operation Result:

One 4 5 6

Java Learning----Reference passing of arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.