java study notes (D) array

Array

Array is a combination of the same data type of a data set, the data manage unified
array itself is a reference data type, the type of storage array may be substantially types may be used type
1. The data array is a reference type
2. array is a string of consecutive addresses are present in the heap memory
4. the length of the array must be specified at initialization
upon heap memory 4. the length of the array is determined not happen again changed
variables the stack memory address stored in the array is referenced
6 array type of internal storage can also be the basic one is cited

1. Define the array (statement)

数据类型[] 数组名字
int[] x;
char[] y;
boolean[] z;
String[] m;

int[] x; ---->比较规范
int x[];
int []x;

2. The array assignment (initialization)

静态初始化 有长度 有元素
     int[] array=new int[]{10,20,30,40,50};
     int[] array={10,20,30,40,50};
     
     int[] array;
     一百行
     array=new int[]{};//此时编译器无法确定变量类型,必须要创建(new)
动态初始化 有长度 没有元素(不是真的没有 默认值)
    int[] array =new int[5];
    整数默认值---0
    浮点数默认值---0.0
    字符型默认值---  0--char 97-a   65-A  48-'0'      
    布尔型默认值---false
    引用数据默认值---null

3. access array elements

通过元素在数组中的位置来访问
位置---->index索引
索引是有取值范围的 [从0开始----数组长度-1]
如果数组的索引超过了上述范围
会出现一个运行时异常 ArrayIndexOutOfBoundsException

4. The array element traversal (polling)

通过循环的方式访问数组的每一个元素
for(;;){
}
JDK1.5版本 javaSE5.0 增加了新的特性: 增强for循环 加强for forEach
for(自己定义的变量(接收数组内每一个元素):遍历数组array){
}

正常的for循环已及加强的for循环都要熟练掌握
1.正常的for  有三个必要条件 index索引 找到某一个元素的位置
        可以通过index直接访问数组的某一个位置 取值 存值都可以
        不好在于写法相对来说比较麻烦
2.增强for    有两个条件 用来取值的变量 用来遍历的数组 没有index索引
        好处写法相对比较容易
        不好只能取值 不能存值
        没有index索引 找不到元素哪一个
public class TestArray{
    public static void main(String[] args) {
         String[] array=new String[5] ;
         for(String value:array){
            System.out.println(value);
         }

//        int[] array=new int[]{10,20,30,40,50};
       //通过元素在数组中的位置index(索引 下标)来访问
       //array[index] index从0开始
       //从数组内取得某一个位置的元素
       //int value =array[0];
       //System.out.println(value);
       //向数组内的某一个位置存入元素
       //array[3] = 400;
       //索引有范围的
       
       //int value=array[5];
       //System.out.println(value);
       //异常 ---运行时异常   
       //ArrayIndexOutOfBoundsException 数组索引越界
       //index索引  0开始----数组(长度-1)结束  
       
       //将数组中的每一个元素都拉出来看一看
     /*
       for(int index=0;index<array.length;index++ ){ //每一次做一样的事情 取数组的值 5次 
          int value =array[index];
          System.out.println(value);
       }    
       System.out.println("-----我是一个华丽的分隔符-----");
       
       for(int value:array){
       System.out.println(value);
       }
       */
    }
}

The difference between the basic data types and the reference data type in the memory structure

All variables are stored in the stack memory space
variable space can store basic data types may be stored reference data type
if the space for storing the variable data storage is the basic value of a variable is changed to another will not follow the change
, if the storage space variable is a reference data type storing the reference value is changed (address) corresponding to the address of a variable along another change
Here Insert Picture Description

public class TestArray2{
    public static void main(String[] args) {
         int a = 10;
         int b=a;
             b=100;
         System.out.println(a);//? 10
 
         int[]x=new int[]{10,20,30};
         //栈内存中的小容器 类型定义了只能存储这种东西 容器中只能存一份
         //见到new 关键字 相当于 在 堆内存中申请开辟一块新的空间
         //数组在堆内存的空间形态 是一串连续的地址
         //基本类型变量空间存储的是值 传递的是值 一个改变 另一个不变
         //引用类型变量空间存储的是地址(引用) 传递的就是引用 一个改变 另一个跟着改变
         int[] y =x;
         y[0] =100;
         System.out.println(x[0]);//?100

    }
}

Demand for container (variable array)

1. Create an array to store the even numbered between 1-100

public class SavaNum {
   public static void main(String[] args) {
        //1.需要创建一个数组
        int[] array =new int[50];
        //2.需要将1-100之间的偶数存入数组内
       for(int i=0;i<array.length;i++) {//执行50次 
          array[i]=2*i+2;
       }
       for(int value:array){
          System.out.println(value);
       }

   }
}
设计程序的时候出现的小问题
 1.创建一个数组    动态初始化????
         元素个数比较少 静态 元素个数很多有规律 动态  元素很多没规律
 2.用了两个循环 一个只为了存值 另一个只为了输出看一看
         存放时直接看结果可能由于存放误操作---看时正确 之后就发生变化
         存放不一定必须要查看 存放是一件事情 查看是另一件

2. Exercise array

0. create an array to store the odd-numbered 1-100
public class SavaNum {
   public static void main(String[] args) {
        //1.需要创建一个数组
        int[] array =new int[50];
        //2.需要将1-100之间的奇数存入数组内
       for(int i=0;i<array.length;i++) {//执行50次 
          array[i]=2*i+1;
       }
       for(int value:array){
          System.out.println(value);
       }

   }
}

1. Given two arrays a {1,2,3,4} b {5,6,7,8} corresponding to the position within the two element arrays swap
public class Practice1 {
        public static void main(String[] args) {
         //给定两个数组a{1,2,3,4} b{5,6,7,8} 将两个数组内的元素对应位置调换
         //1.静态初始化两个数组
          int[] a={1,2,3,4};
          int[] b={5,6,7,8,9};

          //2.元素对应index位置的互换 每一次交换两个数字 4次
          //方式一 交换数组中对应的元素(循环次数好多次 受长度限制)
          /*for(int i=0;i<a.length;i++){
                 a[i]=a[i] ^  b[i];
                 b[i]=b[i] ^  a[i];
                 a[i]=a[i] ^  b[i];     
          }*/
          //方式二 直接交换变量a和b中的数组引用(地址) 没有循环一次搞定 不受长度限制(两个数组长度要相同)
          int[] temp =a; 
          a=b;
          b=temp; 

//        3.输出查看
          for(int i:a){
            System.out.println("a  " + i);
          }
          for(int i:b){
            System.out.println("b  " + i);
          }
       }
}

Here Insert Picture Description


2. Given an array a {1,2,3,4,5,6} the two elements in the array corresponding to the head and tail are reversed
public class Practice2 {
        public static void main(String[] args) {
         //给定一个数组a{1,2,3,4,5,6}将两个数组中的元素头尾对应互换位置
         //1.静态初始化一个数组
          int[] a={1,2,3,4,5,6};
          //2.循环做一件事 将素头尾对应元素调换
          for(int i=0;i<a.length/2;i++){
//               头尾对应互换0<-->5 1<-->4 2<-->3
                 a[i]=a[i] ^  a[a.length-1-i];
                 a[a.length-1-i]=a[a.length-1-i] ^  a[i];
                 a[i]=a[i] ^  a[a.length-1-i];     
          }
//        3.输出查看
          for(int v : a){
            System.out.println(v);
          }
       }
}
3. Given an array a {1,2,3,4,5,6} is calculated the average value of all array elements
public class Practice3{
       public static void main(String[] args) {
          //给定一个数组a{1,2,3,4,5,6}计算所有数组元素的平均值
          //静态初始化一个数组a
          int[] a={1,2,3,4,5,6};
          //计算平均值 1.取出所有以元素相加 2.相加的和除以元素个数即为平均值
          int num=0;
           for(int aa :a ){
              num+=aa;
           }
          System.out.println("平均值为 "+num/a.length); // 21/6 = 3
    }
}
4. Given an array a {1,2,5,7,9,0,2,4,6,8} to find the maximum and minimum array
public class Practice4 {
      public static void main(String[] args) {
            //给定一个数组a{1,2,5,7,9,0,2,4,6,8}找寻数组中的最大值和最小值
            //买一件衬衫  百货商城 最便宜的
            //第一家  200元  记录一下200 (笔记本 脑子里) 
            //第一家  180元  如果比记录的这个便宜 抹去记录信息 重新记录
            //第一家  250元  如果比记录的笔记本数据贵 继续找下一家  
            //第一家  50元   如果比笔记本便宜 抹去 重新记录  笔记本50
            //以上是思路分析
            //静态初始化一个数组
            int[] a = {3,2,5,7,9,1,2,-4,6,8};
     
            //1.创建一个变量 当做笔记本 记录信息
            int max =a[0];   //记录最大     
            int min =a[0];   //记录最小 
            //2.挨个寻找数组中的元素 与变量中的元素进行比较    
            for(int i=0;i<a.length;i++)  {
                 if(a[i]>max){
                    max=a[i];
                 }
                 if(a[i]<min){
                    min=a[i];
                 }
               }
           System.out.println("最大"+max);
           System.out.println("最小"+min);
    }
}

5. Given two arrays a {1,2,3} b {4,5} merge two arrays (5 creates a new array of length)
public class Practice5 {
       public static void main(String[] args) {
            //给定两个数组a{1,2,3} b{4,5}合并两个数组(创建一个新的数组5长度)
            //1.静态初始化两个数组
            int[] a={1,2,3};
            int[] b={4,5};
           //2.因为数组长度一旦确定 不能再次改变  需要创建新数组 长度等于a 和 b的总长度
            int[] newArray =new int[a.length+b.length]; //只有长度 元素默认值是0
             //3.思路二 想将新数组填满
            for(int i=0;i<newArray.length;i++){
               if(i<a.length) {//新数组索引位置还没有a数组长度范围以外
                 newArray[i]=a[i];
               }else{
                 newArray[i] =b[i-a.length];
               }
            }

           //3.思路一 分别将a和b数组的元素存入新数组内
           /* 
            for(int i=0;i<a.length;i++){//将a数组的元素存入新数组内
               newArray[i]=a[i];
            }//newArray--->{1,2,3,0,0}
            for(int i=0;i<b.length;i++){ //将b数组的元素存入后面位置
               newArray[a.length+i] =b[i];
            }*/
            //输出查看
           for(int v:newArray){
              System.out.println("newArray  "+v); 
           }
       }
}
6. Given an array a {1,2,3,9,4,5} in accordance with the position of the maximum in the array, the array split into two {2,3} {4,5}
public class Practice6 {
       public static void main(String[] args) {
            //给定一个数组a{1,2,3,9,4,5}按照数组中的最大值的位置,将数组拆分成两个{1,2,3}{4,5}
            
            //1.需要一个数组 
           int[] oldArray ={1,2,3,9,4,5};

            //2.找寻最大值的索引位置-->为了通过这个位置 确定两个小数组的长度
            int max   =oldArray[0]; //数组的第一个元素值
            int index =0;// 数组的第一个索引的位置
            for(int i=0;i<oldArray.length;i++) {
                  if(oldArray[i]>max){
                     max=oldArray[i];
                     index=i;
                  }
             }
           System.out.println("最大值    "+max);
           System.out.println("最打值位置 "+index);
            //3.需要两个小数组分别承载元素
            int[]  newa =new int[index];
            int[]  newb =new int[oldArray.length-index-1];
            //分别将两个小数组填满
            for(int i=0;i<newa.length;i++)  {
                newa[i]=oldArray[i];
            }
              
            for(int i=0;i<newb.length;i++)  {
                newb[i]=oldArray[(index+i)+1];
            }
           for(int v:newa){
              System.out.println("newa  "+v); 
           }
            for(int v:newb){
              System.out.println("newb  "+v); 
           }
       }
}
7. Given an array of two to remove a {1,2,3,0,0,4,5,0,6,0,7} 0 array element (array to create a new non-zero elements singled out)

Here Insert Picture Description

public class Practice7 {
       public static void main(String[] args) {
            //1.需要一个数组 
           int[] oldArray ={1,2,3,0,4,5,6,0,6,7,8,0,9,10,11};
           //2.找寻原数组中的非零元素个数---->才能确定新数组的长度
           //思路二 创建一个足够长的数组
            int[] newArray=new int[oldArray.length];
            int index=0; //控制新数组的索引变化
            for(int i=0;i<oldArray.length;i++){
                if(oldArray[i]!=0){
                   newArray[index++]=oldArray[i];
                }
            }         
           int[] newArray2=new int[index];

            for(int i=0;i<index;i++){
                   newArray2[i]=newArray[i];
            } 

 /*         int count=0;//记录原数组非零个数
          for(int i=0;i<oldArray.length;i++ ){
             if(oldArray[i]!=0){
               count++;
             }
          }
          System.out.println("原数组非零个数  "+count);
          //3.创建一个新数组 装原数组中的非零元素
          int[] newArray=new int[count];  //扩展二 创建一个足够长的数组
          //4.将原数组中非零元素挑出来 存入新数组
          int index=0; //控制新数组的索引变化
          for(int i=0;i<oldArray.length;i++){
             if(oldArray[i]!=0){
                newArray[index++]=oldArray[i];
               
             }
          }
*/
          //5.旧数组没有用 删掉
          oldArray=null;  //清除引用
          newArray=null;
         //验证新数组
         for(int v: newArray2){
           System.out.println(v);
         }
         
    }
}
8. The memory array of between 2-100 to create a prime (prime number)
public class Practice8 {
      //创建一个数组 存储2-100之间的素数(质数)
       public static void main(String[] args) {
          //思路一  空间占用小 执行效率慢     
         //0.通过一个几千次的循环找寻一个---count
         //1.创建一个数组 长度(刚刚好 没有一个多余的空间)
         //2.通过一个几千次的循环找寻素数 将素数存入数组内
         
         //思路二  执行效率高 空间占用大
         //0.创建一个足够长的数组
         //1.通过一个几千次的循环找寻素数 将素数存入数组内
         //2.将存入素数的数组 后面部分0元素去掉
         //0
         int [] primeNumberArray=new int[50];
          //2.找寻2-100之间素数的个数 将找到的素数存入数组内

          int index=0;//创建一个新的变量 记录素数数组的索引变化 同时记录素数个数
          for(int num=2;num<=100;num++){ //从2--100之间找寻还有没有其他可以被整除的数
              boolean bl = false; //标识 用来记录最初的状态 
              for(int i=2; i<=num/2 ;i++){ 
                 if(num%i==0){  //如果还有能整除的数字 证明num不是素数
                      bl=true; //如果满足条件(找到整除 证明不是素数 改变标识)
                      break;
                  }
               }     
                if(!bl){//如果标识与最初的一致 证明循环内的if从来没有执行过
                   primeNumberArray[index++] =num;
                }

          }  

          int[] newArray =new int[index];

           for(int i=0;i < newArray.length;i++){
               newArray[i]  =primeNumberArray[i];
           } 

          primeNumberArray=null;
          for(int v :newArray){
             System.out.println(v);
          } 

          /*
          //0.找寻2-100之间素数的个数---->确定数组长度
          int count=0; //用来记录素数的个数
          for(int num=2;num<=100;num++){ //从2--100之间找寻还有没有其他可以被整除的数
              boolean bl = false; //标识 用来记录最初的状态 
              for(int i=2; i<=num/2 ;i++){ 
                 if(num%i==0){  //如果还有能整除的数字 证明num不是素数
                      //System.out.println(num+"不是素数");
                      bl=true; //如果满足条件(找到整除 证明不是素数 改变标识)
                      break;
                  }
               }     
                if(!bl){//如果标识与最初的一致 证明循环内的if从来没有执行过
                    //System.out.println(num+"是素数");
                   count++;
                }
          }

         System.out.println("经过找寻 2-100之间的素数个数为 "+count);
          //1.创建一个数组 存素数
          int [] primeNumberArray=new int[count];
           
          //2.找寻2-100之间素数的个数 将找到的素数存入数组内
          int index=0;//创建一个新的变量 记录素数数组的索引变化 
          for(int num=2;num<=100;num++){ //从2--100之间找寻还有没有其他可以被整除的数
              boolean bl = false; //标识 用来记录最初的状态 
              for(int i=2; i<=num/2 ;i++){ 
                 if(num%i==0){  //如果还有能整除的数字 证明num不是素数
                      //System.out.println(num+"不是素数");
                      bl=true; //如果满足条件(找到整除 证明不是素数 改变标识)
                      break;
                  }
               }     
                if(!bl){//如果标识与最初的一致 证明循环内的if从来没有执行过
                    //System.out.println(num+"是素数");
                   primeNumberArray[index++] =num;
                }

          }    
          //输出验证
          for(int v :primeNumberArray){
             System.out.println(v);
          }     
          */ 
    }
}
9. Sort the array elements (bubble quickly select Insert Hill heap sort bucket sort merge sort recursion ...

Here Insert Picture Description

import java.util.Scanner;

public class Practice10 {
       public static void main(String[] args) {
           //1.需要一个数组保存账号和密码 相当于一个小数据库
           String[] userBox ={"java","腾讯课堂","test"};
           int[] passwordBox ={123,666,888} ; 
           //输入账号和密码
           Scanner input =new Scanner(System.in);
           System.out.println("请输入账号");
           String user= input.nextLine();
           System.out.println("请输入密码");
           int password=input.nextInt();
           //3.比较
           boolean bl=false;
           for(int i=0;i<userBox.length;i++){
               if(userBox[i].equals(user)){  //账号存在
                    if(passwordBox[i]==password){  //密码正确
                         System.out.println("登录成功");
                         bl=true;
                    }
                    //账号对了,就结束循环
                    break;  
               }
           }
           if(!bl){
               //用户名 或 密码有一个不对 需要输出
               //bl标记的值与最初的一样 没有改
               System.out.println("用户名不存在或密码错误");
           }
      }
}
10. The user login authentication (user as small inventory value verification data array)
Published 30 original articles · won praise 0 · Views 6657

Guess you like

Origin blog.csdn.net/qq_37710756/article/details/103255716