JAVA基础第三期(方法,数组篇)

方法01

  1. 尽量保持main方法区的整洁度
  2. 方法组成:修饰符,返回值类型,方法名(参数类型,参数名){
    方法体
    return,返回值
    }
  3. 每个方法尽量只写一个功能,这样便于后面的拓展

例如:

public class FangFa01 {
public static void main(String[] args) {
int Fangfa1 = Fangfa1(1,2,3);
System.out.println(Fangfa1);
}
public static int Fangfa1(int a,int b,int c){
int one = a+b+c;
return one;
}

方法覆写

  1. 覆写完后记得改数据类

例如:

public class FangFa01 {
public static void main(String[] args) {
double Fangfa1 = Fangfa1(1.1,2.1,3.1);
System.out.println(Fangfa1);
}
public static int Fangfa1(int a,int b,int c){
int one = a+b+c;
return one;
}
public static double Fangfa1(double a,double b,double c){
double one = a+b+c;
return one;
}
}

数组

  1. int[] One;//声明一个数组
  2. One = new int[10];//定义一个数组 同时声明大小 如果不声明大小系统就不知道你要弄多少个
  3. 数组的元素是通过索引访问的,数组的索引从0开始
  4. 获取数组长度:数组名.length
  5. 想要调取数组要调用索引名
  6. 数组的长度都是固定的,一经创建就不可以改变
  7. 如果看到这个异常:ArrayindexOutofBoundsException:数组下标越界异常 那就代表你输出的数组已经超过你设定的数组

数组分为动态数组和静态数组

  1. 动态数组写作方法:One = new int[10】正常的都是动态方法
  2. 静态数组写作方法:int【 】a = {1,2,3,4}这是静态方法
  3. 二维数组:每一个元素都是一个数组 比如:int[][] a = new int[1][2] 这个数组可以看成一行四列 也可以写成静态数组:int[][] a = {{1,2},{2,3}}

Arrays类的使用方法

public class ShiZu02 {
public static void main(String[] args) {
int[] s = {1,2,3,4,5,9,8,56,55,67,67} ;
//打印数组元素Arrays.toString(s)
System.out.println(Arrays.toString(s));
System.out.println("*************************");
Arrays.sort(s);//数组排列
System.out.println(Arrays.toString(s));
System.out.println("========================");
Arrays.fill(s,0);//数组填充
System.out.println(Arrays.toString(s));
}
}
输出结果如下:
[1, 2, 3, 4, 5, 9, 8, 56, 55, 67, 67]

[1, 2, 3, 4, 5, 8, 9, 55, 56, 67, 67]

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

冒泡排序

//冒泡排序
/*

  • 1.比较数组中两个相邻的元素,如果第一个比第二个大就交换他们的位置
  • 2.每一次比较都会产生一个最大的数或最小的数
  • 3.下一轮将少一个排序
  • 4.依次循环直到结束
  • */

public class ShuZu03 {
public static void main(String[] args) {
int[] a = {1,4,3,5,6,3,6,546,34,6,6,3,34,488};
int[] tai = tai(a);
System.out.println(Arrays.toString(tai));
}
public static int[] tai(int[] a){
//临时变量
int b = 0;
//外层循环判断要跑多少次
for (int i = 0; i <a.length-1 ; i++) {
//内层循环判断两个数,如果第一个数比第二个数大就交换两个数的位置
for (int j = 0; j <a.length-1-i ; j++) {
if (a[j+1]<a[j]){
b = a[j];
a[j] = a[j+1];
a[j+1] = b;

            }
        }
    }
  return a;
}

}
输出结果:[1, 3, 3, 3, 4, 5, 6, 6, 6, 6, 34, 34, 488, 546]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值