javaSE-Day1-数组

数组的定义方式:

		1 intdata[] = new int[x]
               	2 int data[] = new int[] {1, 2, 3, 4 …}

数组的排序: ( java.util.Arrays.sort() )

/*
      public static void sort(int temp[]) {
           for(inti = 0; i < temp.length; i++) {
                 for(intj = 0; j < temp.length - 1; j++) {
                      if(temp[j]> temp[j+1]) {
                            intp = temp[j];
                            temp[j] = temp[j+1];
                            temp[j+1] = p;
                      }
                 }
           }
      }
      */
 

数组的转置:1, 2, 3, 4, 5 >> 5, 4, 3, 2, 1

public staticvoid reverse(int arr[]) {
           intlen = arr.length/2;
           inthead = 0;
           inttill = arr.length - 1;
           for(inti = 0; i < len; i++) {
                 inttemp = arr[head];
                 arr[head] = arr[till];
                 arr[till] = temp;
                 head++;
                 till--;
           }
      }

(!)四

对象数组:将多个对象交给数组统一管理。

class Book {
      private String name;
      private double price;
      public Book(String n, double p) {
           name = n;
           price = p;
      }
      public String getInfo() {
           return"Name: " + name + " price: " + price;
      }
}
 
public class StringArrays {
      public static void main(String args[]) {
           Bookbk[] = new Book[] {
                 newBook("Java", 23.2),
                 newBook("Jsp", 25.3),
                 newBook("Jdk", 53.1)
           };
           for(inti = 0; i < bk.length; i++) {
                 System.out.println(bk[i].getInfo());
           }
 
      }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值