標籤:分析 .so 常用 bsp util 開始 int static 數組轉置
數組的定義形式:
動態初始化方式;
1.聲明並開闢數組
String str[]=new String[3];//3表示數組的長度
2.分布完成
String str[]=null;str=new String[3];
靜態初始化
1.簡化格式
String str [] = {"1","2","3"};
2.完整格式
String str = new String[]{"1","2","3"}
數組的記憶體分析,數組屬於引用傳遞
public class Test{ public static void main(String args[]){ int data[] = new int[3];//開闢一個長度為3的數組 data[0]=1; data[1]=2; data[2]=3;
int temp[] = data;
temp[0] = 99
for(int x=0;x<data.length;x++){
System.out.println(data[x]); } }}
當temp[0]的值改變,則data[0]也會被改變,這就是引用傳遞;
(此處有圖片)
二維數組的定義
public class Test{ public static void main(String args[]){ int data[][] = new int[][]{ {1,2,3}, {4,5,6}, {7,8,9} }; //進行數組的輸出操作 //外層迴圈控制行 for(int x=0;x<data.length;x++){ //內層迴圈控制列 for(int y=0;y<data[x].length;y++){ System.out.print(data[x][y]+"\t"); } System.out.println(); } }}
數組排序中的一個方法
public class Test{ public static void main(String args[]){ int data[] = new int[]{10,25,3,40,8,27,44,17,13,50,2}; print(data); sort(data); print(data); } public static void sort(int arr[]){ for(int x=0;x<arr.length;x++){//外層迴圈主要控制排序的次數 for(int y=0;y<arr.length-1;y++){//內層迴圈主要是進行數組的排序 if(arr[y]>arr[y+1]){ int temp=arr[y]; arr[y] = arr[y+1]; arr[y+1]=temp; } } } } public static void print(int temp[]){ for(int x=0;x<temp.length;x++){ System.out.print(temp[x]+"、"); } System.out.println(); }}
數組的轉置
//數組的轉置public class Test{ public static void main(String args[]){ int data[] = new int[]{1,2,3,4,5,6,7,8,9,10}; print(data); reverse(data); print(data); } public static void reverse(int arr[]){ int head=0; //從頭開始增加 int tail=arr.length-1; //從尾部遞減 for(int x=0;x<arr.length/2;x++){ int t=arr[head]; arr[head] = arr[tail]; arr[tail] = t; tail--; head++; } } public static void print(int temp[]){ for(int x=0;x<temp.length;x++){ System.out.print(temp[x]+"、"); } System.out.println(); }}
二維數組轉置
public class Test{ public static void main(String args[]){ int data[][] = new int[][]{ {1,2,3}, {4,5,6}, {7,8,9} }; print(data); reverse(data); print(data); } public static void reverse(int arr[][]){ for(int x=0;x<arr.length;x++){ for(int y=x;y<arr[x].length;y++){ int t=arr[x][y]; arr[x][y]=arr[y][x]; arr[y][x] = t; } } } public static void print(int temp[][]){ for(int x= 0;x<temp.length;x++){ for(int y=0;y<temp[x].length;y++){ System.out.print(temp[x][y]+"、"); } System.out.println(); } }}
數組中常用的方法
數組拷貝: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(dataA,2,dataB,3,2); print(dataB); } public static void print(int temp[]){ for(int x= 0;x<temp.length;x++){ System.out.print(temp[x]+"、"); } System.out.println(); }}
11、22、33、3、4、66、77、88、99、
數組排序的方法:java.util.Array.sort(數組名稱);
初次接觸對象數組:所謂的對象數組,就是把參考型別的對象通過數組的形式進行儲存
代碼執行個體
class Book{ private String title; private double price; public Book(String title, double price){ this.title=title; this.price=price; } public String toString(){ return "書的名字:" + this.title + ",價格:" + this.price; }}public class Test{ public static void main(String args[]){ Book book[] = new Book[]{ new Book("java",11.3), new Book("javascript",17.3), new Book("html",9.3) }; for(int x=0;x<book.length;x++){ System.out.println(book[x]); } }}
java中的數組概念