最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一篇文章帶你深入了解Java基礎(5)

 更新時間:2021年08月02日 11:48:42   作者:zsr6135  
這篇文章主要給大家介紹了關于Java中方法使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1、數(shù)組

數(shù)組的引用傳遞

public class TestDemo1{
	public static void main(String args[]){
		int data[] = null;
		data = new int [3];
		data[0] = 10;	//第一個元素
		data[1] = 20;	//第二個元素
		data[2] = 30;	//第三個元素
	}
}

image-20210728131418920

public class TestDemo1{
	public static void main(String args[]){
		int data[] = null;
		int temp[] = null;
		data = new int [3];
		data[0] = 10;	//第一個元素
		data[1] = 20;	//第二個元素
		data[2] = 30;	//第三個元素
		temp = data;
		temp[0] = 99;
		for(int i=0 ;i < temp.length ; i++){
			System.out.println(temp[i]);
		}
	}
}

image-20210728131901131

image-20210728131948180

引用傳遞分析都是一個套路,不同的堆被同一個棧內存所指向。

數(shù)組的靜態(tài)初始化

public class TestDemo2{
	public static void main(String args[]){
		//數(shù)組靜態(tài)初始化的兩種方式
		//簡化格式
		int data [] = {1,2,3};
		//完整格式
		int data [] = new int []{1,2,3};
	}
}

數(shù)組的最大缺點:長度固定。

數(shù)組與方法的調用

public class TestDemo2{
	public static void main(String args[]){
		int data [] = new int []{1,2,3,4,5};
		printfArray(data);	//int temp [] = data;
	}
	//定義一個專門用于數(shù)組輸出的方法
	public static void printfArray(int temp[]){
		for(int i = 0; i < temp.length; i++){
			System.out.println(temp[i] + "、");
		}
	}
}

image-20210728134712260

方法返回數(shù)組

public class TestDemo2{
	public static void main(String args[]){
		int data [] = init();	//接受數(shù)組
		printfArray(data);	//int temp [] = data;
	}
	//此時的方法希望可以返回一個數(shù)組類型,所以
	//返回值類型定義為整型數(shù)組
	public static int[] init(){
		return new int []{1,2,3,4,5};
	}
	//定義一個專門用于數(shù)組輸出的方法
	public static void printfArray(int temp[]){
		for(int i = 0; i < temp.length; i++){
			System.out.println(temp[i] + "、");
		}
	}
}

擴大數(shù)組的內容

public class TestDemo2{
	public static void main(String args[]){
		int data [] = init();	//接受數(shù)組
		inc(data);	//擴大數(shù)組的內容
		printfArray(data);	//int temp [] = data;
	}
	//此時的方法希望可以返回一個數(shù)組類型,所以
	//返回值類型定義為整型數(shù)組
	public static int[] init(){
		return new int []{1,2,3,4,5};
	}
	public static void inc(int arr[]){	//沒有返回值
		for(int i = 0 ; i<arr.length ; i++){
			arr[i] *= 2;
		}
	}
	//定義一個專門用于數(shù)組輸出的方法
	public static void printfArray(int temp[]){
		for(int i = 0; i < temp.length; i++){
			System.out.println(temp[i] + "、");
		}
	}
}

image-20210728140205439

image-20210728140509339

Java對數(shù)組的支持

在java本身的類庫中也提供有對于數(shù)組相關的方法。

1、數(shù)組的排序:java.util.Arrays.sort(數(shù)組名稱)

public class TestDemo3{
	public static void main(String args[]){
		int data [] = new int [] {12,3,54,23,64,11};
		java.util.Arrays.sort(data);
		for(int i = 0;i < data.length ; i++){
			System.out.println(data[i]);
		}
	}
}

image-20210728141723999

2、數(shù)組的拷貝:指的是將一個數(shù)組的部分內容替換掉另一個數(shù)組的部分內容

方法:System.arraycopy(源數(shù)組名稱,源數(shù)組開始,目標數(shù)組名稱,目標數(shù)組開始點,拷貝長度);

數(shù)組的數(shù)據(jù)分析

public class TestDemo3{
	public static void main(String args[]){
		int data [] = new int [] {12,3,54,23,64,11};
		int max = data[0];	
		int min = data[0];
		int sum = 0;
		for(int i = 0; i < data.length ; i++){
			sum += data[i]; 
			if(data[i]>max){
				max = data[i];
			}
			if(data[i]<min){
				min = data[i];
			}
		}
		System.out.println("max = " + max);		//求最大值
		System.out.println("min = " + min);		//求最小值
		System.out.println("sum = " + sum);		//求總和
		System.out.println("average = " + sum/(double)data.length);	//求平均值
	}
}

image-20210728143531344

數(shù)組排序

image-20210728150949018

發(fā)現(xiàn)最終要進行循環(huán)的次數(shù)就是N^(n-1),時間復雜度高。

public class TestDemo4{
	public static void main(String args[]){
		int data [] = new int [] {9,8,5,6,4,2,1,0,3,7};
		sort(data);
		printfArray(data);
	}
	public static void sort(int arr[]){//實現(xiàn)數(shù)組的升序排序
		for(int i = 0 ;i < arr.length - 1 ; i++){
		//控制循環(huán)的次數(shù)
			for(int j = 0 ; j < arr.length - i - 1; j++){
				if(arr[j]>arr[j+1]){
					int temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
			}
		}
	}
	//定義一個專門用于數(shù)組輸出的方法
	public static void printfArray(int temp[]){
		for(int i = 0; i < temp.length; i++){
			System.out.println(temp[i] + "、");
		}
	}
}

數(shù)組的轉置

image-20210728170825683

image-20210728170835798

public class TestDemo4{
	public static void main(String args[]){
		int data [] = new int [] {9,8,7,6,5,4,3,2,1,0};
		reverse(data);
		printfArray(data);
	}
	public static void reverse(int arr[]){
		int center = arr.length / 2;	//轉換次數(shù)
		int head = 0;	//頭部索引
		int tail = arr.length - 1;	//尾部索引
		for(int i = 0 ; i < center ; i++){
			int temp = arr[head];
			arr[head] = arr[tail];
			arr[tail] = temp;
			head ++;tail --;
		}
	}
	//定義一個專門用于數(shù)組輸出的方法
	public static void printfArray(int temp[]){
		for(int i = 0; i < temp.length; i++){
			System.out.print(temp[i] + "、");
		}
	}
}

image-20210728171451639

public class TestDemo5{
//二維數(shù)組轉置
	public static void main(String args[]){
		int data [][] = new int [][] {{9,8,7},{6,5,4},{3,2,1}};
		reverse(data);
		printfArray(data);
	}
	public static void reverse(int arr[][]){
		int count = arr.length;	//轉換次數(shù)
		System.out.println(count);
		for(int i = 0 ; i < arr.length ; i++){
			for(int j = i; j < arr.length; j++){
				if(i != j){
					int temp = arr[i][j];
					arr[i][j] = arr[j][i];
					arr[j][i] = temp;
				} 
			}
		}
	}
	//定義一個專門用于數(shù)組輸出的方法
	public static void printfArray(int temp[][]){
		for(int i = 0; i < temp.length; i++){
			for(int j = 0 ; j < temp[i].length ; j++){
				System.out.print(temp[i][j] + "、");
			}
				System.out.println();
		}
	}
}

image-20210728193520526

數(shù)組的二分查找法

要求你在一個指定的數(shù)組之中查詢一個數(shù)據(jù)的位置。

普通的查找的時間復雜度是n.

public class TestDemo6{
//二分查找
	public static void main(String args[]){
		int data [] = new int [] {1,2,3,4,5,6,7,8,9,10};
		int search = 9;
		System.out.println(binarySearch(data, 0 , data.length-1, search));
	}
	public static int binarySearch(int arr[],int form, int to, int key){
		if(form < to){
		int mid = (form / 2) + (to / 2);	//確定中間位置索引
			if(arr[mid] == key){
				return mid;
			}else if(key > arr[mid]){
				return binarySearch(arr, mid+1 , to , key);
			}else if(key < arr[mid]){
				return binarySearch(arr, form, mid-1, key);
			}
		}
		return -1;
	}
}

image-20210728201547413

對象數(shù)組

之前所接觸的都是基本數(shù)據(jù)類型的數(shù)據(jù),那么對象也可以將其定義為數(shù)組,這樣操作形式叫做對象數(shù)組。對象數(shù)組往往是引用數(shù)據(jù)類型為主的定義,例如類、接口,而且對象數(shù)組分為兩種定義格式。

class Person{
	private String name;
	private int age;
	public Person(String n, int a){
		name = n;
		age = a;
	}
	public void setName(String n){
		name = n;
	}
	public void setAge(int a){
		age = a;
	}
	public String getName(){
		return name;
	}
	public int getAge(){
		return age;
	}
	public String getInfo(){
		return "name = " + name + ",age = " + age;
	}
}
public class TestDemo7{
//對象數(shù)組
	public static void main(String args[]){
		Person per [] = new Person [3];	//動態(tài)初始化
		Person per1 [] = new Person [] {
			new Person("張三",22),
			new Person("張三1",22),
			new Person("張三2",22)
		};	//靜態(tài)初始化
		per[0] = new Person("張三",22);
		per[1] = new Person("李四",30);
		per[2] = new Person("王五",13);
		for(int i = 0;i < per.length ; i++){
			System.out.println(per[i].getInfo());
		}
		System.out.println();
		for(int i = 0;i < per.length ; i++){
			System.out.println(per1[i].getInfo());
		}
	}
}

image-20210728203337476

image-20210728203739919

總結

本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!

相關文章

  • 解析Java的Spring框架的BeanPostProcessor發(fā)布處理器

    解析Java的Spring框架的BeanPostProcessor發(fā)布處理器

    這篇文章主要介紹了Java的Spring框架的BeanPostProcessor發(fā)布處理器,Spring是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • Java 動態(tài)數(shù)組的實現(xiàn)示例

    Java 動態(tài)數(shù)組的實現(xiàn)示例

    Java動態(tài)數(shù)組是一種可以任意伸縮數(shù)組長度的對象,本文主要介紹了Java 動態(tài)數(shù)組的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Java中的synchronized有幾種加鎖方式(實例詳解)

    Java中的synchronized有幾種加鎖方式(實例詳解)

    在Java中,synchronized關鍵字提供了內置的支持來實現(xiàn)同步訪問共享資源,以避免并發(fā)問題,這篇文章主要介紹了java的synchronized有幾種加鎖方式,需要的朋友可以參考下
    2024-05-05
  • Java圖形化編程之JFrame疫苗接種系統(tǒng)詳解

    Java圖形化編程之JFrame疫苗接種系統(tǒng)詳解

    GUI圖形界面設計是用戶和程序交互的工具,用戶通過圖形界面控制程序事件的發(fā)生。首先介紹Swing的基本體系結構,這是底層
    2021-09-09
  • SpringBoot中自定義注解實現(xiàn)參數(shù)非空校驗的示例

    SpringBoot中自定義注解實現(xiàn)參數(shù)非空校驗的示例

    這篇文章主要介紹了SpringBoot中自定義注解實現(xiàn)參數(shù)非空校驗,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-11-11
  • Android Home鍵監(jiān)聽的實現(xiàn)代碼

    Android Home鍵監(jiān)聽的實現(xiàn)代碼

    這篇文章主要介紹了Android Home 鍵監(jiān)聽的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Spring Boot接口設計防篡改、防重放攻擊詳解

    Spring Boot接口設計防篡改、防重放攻擊詳解

    這篇文章主要給大家介紹了關于Spring Boot接口設計防篡改、防重放攻擊的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • SpringBoot實現(xiàn)反向代理的示例代碼

    SpringBoot實現(xiàn)反向代理的示例代碼

    本文主要介紹了SpringBoot實現(xiàn)反向代理的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • Maven本地緩存清理小工具的實現(xiàn)

    Maven本地緩存清理小工具的實現(xiàn)

    這篇文章主要介紹了Maven本地緩存清理小工具的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • mybatis-plus雪花算法增強idworker的實現(xiàn)

    mybatis-plus雪花算法增強idworker的實現(xiàn)

    今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,進一步增強實現(xiàn)生成分布式唯一ID,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-07-07

最新評論

西乌| 历史| 扶余县| 尉氏县| 温宿县| 图木舒克市| 新乐市| 吉林省| 陈巴尔虎旗| 化隆| 宝山区| 浮山县| 句容市| 丰镇市| 桐梓县| 金寨县| 安泽县| 延津县| 布尔津县| 铜鼓县| 阿拉善左旗| 东方市| 繁昌县| 恭城| 榕江县| 禹州市| 奎屯市| 临猗县| 石狮市| 庄浪县| 扬州市| 琼海市| 宜兰县| 富川| 广宗县| 平顶山市| 西宁市| 北票市| 西丰县| 西吉县| 西青区|