Java_数组续集

数组的引用传递
数组本身属于引用数据类型,那么既然是引用数据类型就一定可以发生引用传递,而本次将采用方
法处理引用数据类型。
例如:

public class TestDemo {
public static void main(String args[]) {
// 动态开辟了10个长度的数组
int data [] = new int [10] ;
// int [] arr = data ; // 引用传递
init(data) ; // int [] arr = data
print(data) ;
}
// 专门定义一个初始化数组数据的方法
// 要求数组中的每个内容为其索引内容
// 现在定义的参数就表示接收数组
public static void init(int [] arr) {
for (int x = 0 ; x < arr.length ; x ++) {
arr[x] = x ; // 数组内容为其索引内容
}
}
public static void print(int temp[]) {//或者[]temp
for (int x = 0 ; x < temp.length ; x ++) {
System.out.print(temp[x] + "\t") ;
}
}
}

数组的拷贝和排序操作
1.拷贝:
System.arraycopy(源数组名称,源数组开始点,目标数组名称,目标数组开始点,长度)

public class TestDemo
{
     public static void main(String args[])
     {
          int dataA []=new int []{1,2,3,4,5,6,7,8,9};
          int dataB []=new int []{11,22,33,44,55,66,77,88,99};
          System.arraycopy(dataB,3,dataA,4,3);
          for(int x=0;x<dataA.length;x++)
          {
               System.out.print(dataA[x]+"\t");
          }
     }
}

2.排序(1)://非笔试

public class TestDemo
{
     public static void main(String args[])
     {
          int data[]=new int []{8,1,3,0,6};
          java.util.Arrays.sort(data);
          for(int x=0;x<data.length;x++)
          {
           System.out.println(data[x]);
          }
     }
}

3.排序(2):

public class TestDemo
{
     public static void main(String args[])
     {
          int data []=new int []{4,6,3,5,8,0,2};
          for(int x=0;x<data.length;x++)
          {
               for(int y=0;y<data.length-1;y++)
               {
                    if(data[y]>data[y+1])
                    {
                    int temp=data[y];
                    data[y]=data[y+1];
                    data[y+1]=temp;
                    }
               }
               //print(data);
          }
          print(data);
     }
     public static void print(int []temp)
     {
          for(int x=0;x<temp.length;x++)
          {
               System.out.print(temp[x]+"\t");
          }
          System.out.println();
     }
}

注:也可以通过java.util.Arrays.sort()排序。

对象数组
1.对象数组的动态初始化
类名称 对象数组名称 [] = new 类名称 [长度] ;
动态初始化后的数组内容都是其对应数据类型的默认值 null;

class Book
{
     private String title;
     private double price;
     public Book(String title,double price)
     {
          this.title=title;
          this.price=price;
     }
     public String getInfo()
     {
          return "书名:"+this.title+"价格:"+this.price;
     }
}
public class TestDemo
{
     public static void main(String args[])
     {
          Book books[]=new Book[3];
          books[0]=new Book("java",79.8);
          books[1]=new Book("oracle",67.8);
          books[2]=new Book("jsp",74.8);
          for(int x=0;x<books.length;x++)
          {
               System.out.println(books[x].getInfo());
          }
     }
}

2.对象数组的静态初始化
类名称 对象数组名称 [] = new 类名称 [] {实例化对象, …}

class Book
{
     private String title;
     private double price;
     public Book(String title,double price)
     {
          this.title=title;
          this.price=price;
     }
     public String getInfo()
     {
          return "书名:"+this.title+"价格:"+this.price;
     }
}
public class TestDemo
{
     public static void main(String args[])
     {
          Book books[]=new Book[]
          {
             new Book("java",79.8),
             new Book("oracle",67.8),
             new Book("jsp",74.8),
          };
          for(int x=0;x<books.length;x++)
          {
               System.out.println(books[x].getInfo());
          }
     }
}

对于主方法

public class TestDemo
{
     public static void main(String args[])
     {
          for(int x=0;x<args.length;x++)
          {
               System.out.println(args[x]);
          }
     }
}

Over~Good morning!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值