JAVA作用域和排序算法介绍

一、作用域

  1、作用域的概念

       所谓的作用域是指引用可以作用到的范围。

       一个引用的作用域是从引用定义位置到包裹它的最近的大括号的结束位置。只有在作用域范围内才可以访问到引用,超出作用域无法访问引用。

     定义多个同名字的引用,作用不可重叠,如果重叠,编译会报错。

  2、作用域实例

public class Demo06{
    public static void main(String[] args) { 
      {
          int i = 2;  //作用域仅限该大括号内
      }
      System.out.println(i);
    if(true){
        int i = 1;
        System.out.println(i);
    }        
   }
}

二、数组

 1、数组的概念

      数组是长度固定,内容可变的存储多个同一类型数据的容器。

2、定义数组

  a、方式一

   类型[] 引用名称 =new 类型[长度];

  **其中引用中的 [] 可以出现在引用名之前或之后

   这种方式定义的数组,只定义了长度,没有指定初始值,则初始值采用默认值。数值类型为0;char类型为‘a’;boolean类型的为false,引用类型的为null。

  b、方式二

   类型 [] 引用名称 = new 类型[]{数组的初始内容};
   **其中引用中的[]可以出现在引用名之前或之后

   这种方式定义的数组,指定了数组的初始值,而数组的长度等于元素的个数。

c、方式三

 类型[] 引用名称 = {数组的初始内容};

**其中引用中的[]可以出现在引用名之前或之后;

这种方式定义的数组,指定了数组的初始值,而数组的长度等于元素的个数。

实例:

public class Demo07{
    public static void main(String args[]){
        //1.定义数组方式一
        
        int [] nums1 = new int[5];
        int nums2 [] = new int[5];
        char [] cs = new char[5];
        boolean [] bs = new boolean[5];
        String [] strs = new String[5];
        System.out.println(nums1[0]);
        System.out.println(cs[0]);
        System.out.println(bs[2]);
        System.out.println(strs[3]);
        

        //2.定义数组方式二
        int [] nums = new int[]{1,3,5,7,9};

        //3.定义数组方式三
        int [] nums = {2,4,6,8,10};
    }
}

3.数组的基本操作

  a、获取数组中的元素

    通过数组引用【元素的索引】就可以访问到数组的元素。所谓的索引就是元素在数组中的位置,索引由0开始,最大的索引是数组的长度 - 1。

 获取数组的元素方式见上例子。

  b、获取数组的长度

   通过访问数组的length属性,可以得到数组的长度。

//2.获取数组的长度
int [] nums = {1,3,5,7,9};
System.out.println(nums.length);

c、遍历数组

   两种方式:for循环和增强for循环。

//3.遍历数组 - 普通for
int [] nums = {1,3,5,7,9,11,13};
for(int i=0;i<nums.length;i++){
  System.out.println(nums[i]);
} 

//4.遍历数组 - 增强for循环
int [] nums = {1,3,5,7,9};
for(int x : nums){
System.out.println(x); 
}

d、修改数组中的元素

//5.修改数组中的元素
int [] nums = {1,3,5,7,9};
for(int x : nums){
   nums[2] = 100;
  System.out.print(x+" ");
}

4、数组常用应用

  a、获取数组中的最大最小值

//6.获取数组中 最大值/最小值
int [] nums = {-23,-5,-3,-234,-2};
 if(nums.length > 0) {
int max = nums[0];
for(int x :nums) {
if(x > max) {
max = x;
}
}
}else {
  System.out.println("数组为空!");
}

   b、查找数组中元素所在位置

  

//7.查找指定元素所在的位置

int [] nums = {2,3,65,23,6,3,78,9};
int find = 3;

for(int i = 0;i<nums.length; i++) {

   if(find == nums[i]) { 

      System.out.println("数字"+find+"出现在数组"+i+"位");

    }

}

   c、翻转数组

//8.反转数组 0--4 1--3 2 -- 2 3 -- 1 4 --0
int nums [] = {2,3,65,23,6};
int nums2 [] = new int[nums.length];
for(int i=0;i<nums.length;i++){
   nums2[nums.length - 1 - i] = nums[i];
} 
for(int x : nums2){
System.out.println(x); 
}

5、数组排序

   a、常见排序算法及其效率

      

   b、冒泡排序:将数组中的数据从第一位开始依次比较,放到0~length-1处。

    

       //1.冒泡排序
        int nums [] = {1,34,56,8,-32,7,-9,0,235};
        for(int i=0;i<nums.length;i++){//当前要确定的是哪个位置的数
            for(int j =nums.length-1;j>=i+1;j--){
                if(nums[j-1]>nums[j]){
                    int tmp = nums[j-1];
                    nums[j-1] = nums[j];
                    nums[j] = tmp;
                }
            }
        }
        for(int x : nums){
            System.out.println(x);
        }

c、选择排序:将数组中的数据下标拿出来,比较该下标处数组的值和其余值的大小;如果比其他值大,则交换二者下标的值;并将二者的值相互交换。

  原理图:

    实例代码:

//2.选择排序
        int nums [] = {1,34,56,8,-32,7,-9,0,235};
        for(int i=0;i<nums.length;i++){
            int tmp = i;
            for(int j=i+1;j<nums.length;j++){
                if(nums[j]<nums[tmp]){
                    tmp = j;
                }
            }
            //换 tmp 和 i 指向的数
            int x = nums[tmp];
            nums[tmp] = nums[i];
            nums[i] = x;
        }
        
        for(int x : nums){
            System.out.println(x);
        }

猜你喜欢

转载自www.cnblogs.com/chhyan-dream/p/10659313.html