数组的入门性学习 ( 点击 )
用java实现杨辉三角【 倒 / 正 】
接下来呢这篇写的就是关于数组的一些小demo用于理解。
- ArrayMaxDemo1 利用数组找出最大数
- ArrayMinDemo2 利用数组找出最小数
- MulArrayDemo03 冒泡排序
- 求Fibonacci数列前N个数
- ArrayMaxDemo1 利用数组找出最大数:
public class ArrayMaxDemo1 {
public static void main(String[] args) {
int[] array = { 5, 15, 30, 20, 10000, 30, 35 };
int max = array[0]; //
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
System.out.println("最大值:" + max);
}
}
- ArrayMinDemo2利用数组找出最小数
public class ArrayMinDemo2 {
public static void main(String[] args) {
int[] array = { 33324, 5536, 30783, 76670, 10000, -243260, 30082, 325 };
int min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
System.out.println("最小值:" + min);
}
}
- MulArrayDemo03冒泡排序
public class BubbleSort{
public static void main(String[] args) {
int arr[] = {24,69,80,57,13, -100, -1, 0, 0, 9999, 12, 1};
int temp = 0;
for(int i = 0; i < arr.length - 1; i++) {
for( int j = 0; j < arr.length - 1 - i ; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for( int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + "\t");
}
int temp = 0;
for( int j = 0; j < 4; j++) { // arr.length - 1 - 0
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
for( int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + "\t");
}
for( int j = 0; j < 3; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.println();
//输出
for( int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + "\t");
}
for( int j = 0; j < 2; j++) {
//比较
if(arr[j] > arr[j + 1]) {
//交换
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.println();
//输出
for( int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + "\t");
}
for( int j = 0; j < 1; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.println();
for( int j = 0; j < arr.length; j++) {
System.out.print(arr[j] + "\t");
}
}
}
========================================
/**
* 数组高级冒泡排序代码
*/
public static void main(String[] args) {
int[] arr = {24, 69, 80, 57, 13};
bubbleSort(arr);
print(arr);
}
/*
* 冒泡排序
* 1,返回值类型,void
* 2,参数列表,int[] arr
*
* 第一次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3],arr[3]与arr[4]比较4次
第二次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3]比较3次
第三次:arr[0]与arr[1],arr[1]与arr[2]比较2次
第四次:arr[0]与arr[1]比较1次
*/
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) { //外循环只需要比较arr.length-1次就可以了
for (int j = 0; j < arr.length - 1 - i; j++) { //-1为了防止索引越界,-i为了提高效率
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j+1] = temp;
}
}
}
}
/*
* 打印数组
* 1,返回值类型void
* 2,参数列表int[]arr
*/
public static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
【 附 加 拓 展:】
- 求Fibonacci数列前N个数
public class Fibonaccidemo01 {
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
for (int i = 1; i <= num; i++)
System.out.print(getFib(i) + " ");
System.out.println();
}
// 后面的数等于前面二个数的和
public static int getFibRec(int n) {
if (n < 0)
return 0;
if (n <= 2)
return 1;
else
return getFibRec(n - 2) + getFibRec(n - 1);
}
public static int getFib(int n) {
if (n < 0)
return 0;
if (n <= 2)
return 1;
int f = 1;
int f1 = 1;
int f2 = 1;
for (int i = 3; i <= n; i++) {
f = f1 + f2;
f1 = f2;
f2 = f;
}
return f;
}
}