递归算法
定义
递归结构
留神:
1、在程序中能不应用就不应用,应用递归回加大资源耗费,如果应用递归的档次太深,容易造成栈溢出
2、如果不应用递归就无奈解决问题的话,那就必须应用递归,比方:输入某个磁盘目录下的所有文件名称
总结
数组
概述
定义
数组是雷同类型数据的有序汇合
.雷同类型的若干个数据,依照肯定先后秩序排列组合而成
.其中,每一个数据称作一个数组元素
.每个数组元素能够通过一个下标来拜访他们
特点
.其长度是确定的。数组一旦被创立,他的大小就是不能够扭转的。
.其元素必须是雷同类型,不容许呈现混合类型
.数组中的元素能够是任何数据类型,包含根本类型和援用类型
数组属于援用类型
.length,elements of the array
一维数组
应用
申明数组:
调配空间:
数组赋值:
数据处理:
![上传中…]()
public static void main(String[] args) { //申明 int[] array; //创立内存空间 array = new int[5]; //数组赋值 array[0] = 1; array[1] = 2; array[2] = 3; array[3] = 4; array[4] = 5; System.out.println(array[0]); //素组的几种创立形式 //1、申明并申请空间 int[] arr = new int[5]; int [] arr3 = new int[5]; int arr4[] = new int[5]; //2、申明数组并赋值 int[] arr2 = new int[]{1,2,3,4,5}; int[] arr5 = {1,2,3}; }
二维数组
![上传中…]()
pac<em style="color:transparent">来源gao.dai.ma.com搞@代*码网</em>kage com.msbline.basic; public class TwoArray { public static void main(String[] args) { int[][] arr = new int[3][]; arr[0] = new int[5]; arr[1] = new int[3]; arr[2] = new int[4]; //赋值 arr[0][0] = 1; arr[0][1] = 2; arr[0][2] = 3; arr[0][3] = 4; arr[0][4] = 5; arr[1][0] = 6; arr[1][1] = 7; arr[1][2] = 8; arr[2][0] = 9; arr[2][1] = 10; arr[2][2] = 11; arr[2][3] = 12; for(int i = 0; i<arr.length; i++){ for(int j = 0; j<arr[i].length; j++){ System.out.print(arr[i][j] + "t"); } System.out.println(); } } }
排序算法(针对数组的练习)
package com.msbline.basic; public class ArraySort { public static void main(String[] args) { int[] array = new int[]{4,1,7,2,9,3,5,8,6}; //冒泡排序 // for(int i = 0; i< array.length; i++){ // for(int j = 0; j < array.length-1-i; j++){ // if (array[j] > array[j+1]){ // int temp = array[j]; // array[j] = array[j+1]; // array[j+1] = temp; // } // } // } // print(array); //抉择排序 for(int i = 0; i< array.length; i++){ for(int j = i+1; j < array.length; j++){ if (array[i] > array[j]){ int temp = array[i]; array[i] = array[j]; array[j] = temp; } } } print(array); } public static void print(int[] array){ for(int i = 0; i< array.length; i++){ System.out.print(array[i]+"t"); } } }
总结:
java Arrays API的应用