首页 > 代码库 > Java基础总结2

Java基础总结2

Java基础总结2


  • 数组

格式1

元素类型 [] 数组名 = new 元素类型 [元素个数或数组长度]

示例:int [] arr = new int [5]

格式2

元素类型 [] 数组名 = new 元素类型 [] {元素、元素、元素………..}

示例:int [] arr = new int [5] {1,2,3,4,5}

格式3:元素类型 [] 数组名 = {元素、元素、元素………..}

示例:int [] arr = {1,2,3,4,5}

打印数组角标上的值、遍历数组、打印数组长度。

class shuzu

{

      publicstatic void main(String[] args)

      {

                    int[] arr = new int [] {1,2,3,4,5};

                    System.out.println("arr数组1角标上的值="+arr[1]);


                    int[] ar = new int [5];

                    System.out.println("ar数组1角标上的值="+ar[1]);


                    for(int x=0;x<=arr.length-1;x++ )

                    {

                                  System.out.println("遍历数组"+arr[x]);

                    }


                    System.out.println("数组长度"+arr.length);


      }

}

int类型数组中的最大值

示例1

class  shuzu

{

              publicstatic void main(String[] args)

              {

                            int[] arr = new int [] {100,82,96,84,5};//定义一个数组

 

                            intmax = arr[0];//定义一个值用来和其他值比较。

 

                            for(int x=1;x<arr.length ;x++ )//使用for循环遍历数组

                            {

                                          if(max<arr[x])

                                          {

                                                        max=arr[x];//使用max变量记住最大值

                                          }                                        

                            }            

              System.out.println(max);

              }

}

示例2;通过函数的方式定义一个获取任意int类型数组最大值

class  shuzu

{

              publicstatic void main(String[] args)

              {

                            int[] arr = new int [] {100,82,96,84,5};

                            intmax = getmax(arr);//定义一个值,用来比较,并且调用下面的getmax函数,同时把数组arr传入到该函数。

                            System.out.println(max);

              }

 

 

              publicstatic int getmax (int [] arr )

              {

                            intmax = arr[0];

 

                            for(int x=1;x<arr.length ;x++ )

                            {

                                          if(max<arr[x])

                                          {

                                                        max=arr[x];

                                          }                                        

                            }            

                            returnmax;//返回一个最大值。

              }

}

 

数组排序

示例1:选择排序

思路:

1、要将无序数组排序,首先需要将数组打印出来。

2、建立一个打印数组的方法。

3、建立一个排序的方法,首先要排序就要让数组中的元素进行比较,并且需要将小值放到前面,也就是调换位置,可以通过第三方变量的方式来实现元素的调换。

class  shuzu

{

              publicstatic void main(String[] args)

              {

                            int[] arr = {1,2,4,6,3,5,7,10,9,8};

                            paixu(arr);//调用排序方法

                            printarr(arr);//调用打印数组方法

 

                           

 

              }

 

              publicstatic void paixu (int [] arr)

              {

 

                            for(int x=arr[0];x<arr.length ;x++ )

                            {

                                          for(int y=x;y<arr.length ;y++ )

                                          {

                                                        if(arr[x]>arr[y])

                                                        {

                                                                      inttemp =arr[x];

                                                                      arr[x]=arr[y];

                                                                      arr[y]=temp;

                                                        }

                                }

                            }            

               }

 

               public static void printarr (int [] arr)

               {

                                          for(int x=0;x<arr.length ;x++ )

                                          {

                                                        System.out.print(arr[x]+" ");

                                          }                                        

               }

}

示例2:冒泡排序

class  shuzu

{

              publicstatic void main(String[] args)

              {

                            int[] arr = {1,2,4,6,3,5,7,10,9,8};

                            paixu(arr);//调用排序方法

                            printarr(arr);//调用打印数组方法

              }

 

              publicstatic void paixu (int [] arr)

              {

 

                            for(int x=0;x<arr.length-1 ;x++ )//-1表示最后一个值不用比较

                            {

                                          for(int y=0;y<arr.length-x-1 ;y++ )//-x表示每次少比较1

                                          {

                                                        if(arr[y]>arr[y+1])

                                                        {

                                                                      inttemp =arr[y];

                                                                      arr[y]=arr[y+1];

                                                                      arr[y+1]=temp;

                                                        }

                                }

                            }            

               }

 

               public static void printarr (int [] arr)

               {

                                          for(int x=0;x<arr.length ;x++ )

                                          {

                                                        System.out.print(arr[x]+" ");

                                          }

                                         

               }

}

数组的查找

示例1:普通遍历查找

class chazhao

{

              publicstatic void main(String[] args)

              {

                            int[] arr = {2,3,4,1,8,6};

                            intc=cz (arr,6);//调用查询方法,并且把arr数组和需要查找的key传入到查找方法中。

                            System.out.println(c);

              }

 

              publicstatic int cz(int [] arr , int key)//定义一个查询方法。

              {

                            for(int x=0;x<arr.length ;x++ )

                            {

                                          if (arr[x]==key)//定义判断条件

                                          {

                                                        returnx;//满足条件返回该值

                                          }                                        

                            }

              return-1;//不满足条件返回-1

              }

}

示例2:折半查找

折半查找只能查找有序的数组

示例1:使用while循环语句直接判断条件查找 

class  sz

{

              publicstatic void main(String[] args)

              {

                            int[] arr = {1,3,5,7,10,11,12,13,14,16};

                            inta=cz (arr,-10);//调用cz方法查询arr数组中-10的角标

 

                            System.out.println("a="+a);

              }

 

              publicstatic int cz (int [] arr,int key)

              {             int min = 0;//定义一个最小值

                            intmax = arr.length-1;//定义一个最大值,最大值为数组长度-1

                            intmid = (min+max)/2;//定义中间值,中间值为最小值加最大值除以2

 

                            while(key!=arr[mid])//定义循环条件,如果key=中间值时则直接返回中间值。

                            {

                                          if(key>arr[mid])//判断key是否大于中间值时

                                          {

                                                        min=mid+1;//如果大于中间值,最小值等于中间值加1,游标向右移动

                                          }

 

                                          elseif (key<arr[mid])//判断key是否小于中间值

                                          {

                                                        max=mid-1;//如果小于中间值,则最大值等于中间值减1,游标向左移动。

                                          }

                                         

                                          if(min>max)//防止游标越界

                                          {

                                                        return-1;//表示超出范围

                                          }

 

                                          mid=(min+max)/2;//再次进行折半查找

                                                       

                            }

                            returnmid;

              }

 

}

示例

相对示例1简化了代码,并且减少了循环次数。

 class  sz

{

              publicstatic void main(String[] args)

              {

                            int[] arr = {1,3,5,7,10,11,12,13,14,16};

                            inta=cz (arr,10);//调用cz方法查询arr数组中-10的角标

 

                            System.out.println("a="+a);

              }

 

              publicstatic int cz (int [] arr,int key)

              {

                            intmin = 0;//定义一个最小值

                            intmax = arr.length-1;//定义一个最大值,最大值为数组长度-1

                            intmid = (min+max)/2;//定义中间值,中间值为最小值加最大值除以2

                            while(min<=max)//首先判断是否越界,如果最小值大于最大值则直接返回-1

                            {

                                          mid= (min+max)/2;//先将数组折半

                                          if(key>arr[mid])//判断key值游标方向

                                          {

                                                        min=mid+1;

                                          }

                                          elseif (key<arr[mid])//判断key值游标方向

                                          {

                                                        max=mid-1;

                                          }

                                          else

                                          returnarr[mid];//如果满足条件返回对应值。

                            }

              return-1;//如果越界的话直接返回-1

              }

}

使用数组编写进制转换程序:

示例1:简单二进制转换

思路:

1、十进制转换到二进制是一个模以2,除以2的过程。

2、创建一个函数,实现任意十进制数转换2进制,需要定义一个num变量。

3、创建一个循环,用来实现不断模以2除以2的过程。并使用打印语句将模以2的结果打印出来。

              publicstatic void te (int num)

              {

                                          StringBuffersb = new StringBuffer ();//定义一个容器,来存贮模以2的值,并实现反转

                            while(num>0)//定义循环条件,当前函数只能实现正数的二进制转换。

                            {                          

                                          System.out.println(num%2);//打印的模以二的数值就是二进制数,但结果是倒的。

                                          sb.append(num%2);

                                          num=num/2;

                            }            

                            System.out.println(sb.reverse());//打印存进这个容器中的数值,并反转。

              }

示例2十六进制转换

十进制==>>十六进制

取最低4位,算一个值,再取下一个四位,算一个值

60的二进制:0000-0000 0000-0000 0000-0000 0011-1100

 

          0000-0000 0000-0000 0000-0000 0011-1100  ==>>60

   &0000-0000 0000-0000 0000-0000 0000-1111 ==>>15

  -----------------------------------------

     0000-00000000-0000 0000-0000 0000-1100 ==>>12==>>C

 

     0000-00000000-0000 0000-0000 0011-1100 >>>无符号右移

          0000-0000 0000-0000 0000-0000 0000-0011右移后等于3

 

          60的十六进制为3C

 

          因为右移8次,所以循环需要8

 

class jz

{

              publicstatic void main(String[] args)

              {

                            sl(16);               

              }

                            publicstatic void sl (int num)

              {

                            StringBufferab = new StringBuffer ();//定义Stringbuffer容器来存储数据,

                            for(int x=0;x<8;x++ )//因为每次右移4位,需要移动8次。0000-0000 0000-0000 0000-0000 0000-0000

                            {

                                          inttemp =num &15;

                                          if(temp>9)

                                           //        System.out.print((char)(temp-10+‘A‘));

                 ab.append ((char)(temp-10+‘A‘));//将运行结果储存到容器中

                                          else

                                                        //        System.out.print(temp);

                            ab.append(temp);//将运行结果储存到容器中

                                          num=num>>> 4;

                            }

              System.out.println(ab.reverse());//将储存到容器中的结果反转打印出来。

              }

}

示例3、查表法的16进制转换

class jz

{

              publicstatic void main(String[] args)

              {

                            sl3(60);                           

              }

 

              publicstatic void sl3 (int num)

              {

                            //定义一个数组,存储16进制中多涉及的数字。

                            char[] chs={‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,

                                                                       ‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,

                                                                       ‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘};

                            //定义一个8个元素的数组。

                            char[] a =new char[8];

                            intpost =0;

 

                            while(num!=0)

                            {

                                          inttemp = num&15;

                                          a[post++]= chs[temp];//先赋值给post++

                                          num= num >>>4;

                            }

                            for(int x=post-1;x>=0 ;x--)

                            {

                                          System.out.print(a[x]);

                            }

              }

/*

1、定义数组,数组包含16进制数中的所有元素

2、定义一个数组用来存储转换后的数值

3、定一个查找的指针,确定临时数据存储的位置。

4、使用for语句遍历存储后的数组,并打印。

*/

}

示例4、进制转换方法

实现十进制到二进制、八进制、十六进制的转换。

思想:

1、  进制转换是通过循环&一个数,并且通过右移来进行计算的。

2、  建立方法,首先先例一个进制转换的方法,并定义变量,包括num,要&的基数以及需要右移的位数。

class  jz2

{

              publicstatic void main(String[] args)

              {

                            er(60);

                            sl(60);

                            ba(60);

             

              }

 

                           

              publicstatic void er (int num)

              {

                            sl3(num,1,1);

              }

 

              publicstatic void sl (int num)

              {

                            sl3(num,15,4);                            

              }

 

              publicstatic void ba (int num)

              {

                            sl3(num,7,2);

              }

             

              publicstatic void sl3 (int num , int base , int bit)

              {

                            //定义一个数组,存储16进制中多涉及的数字。

                            char[] chs={‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,

                                                                       ‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,

                                                                       ‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘};

 

                            char[] arr = new char [32];//存储数据

 

                            intpost =0;//定义指针,确定临时数组中的存储位置

 

                            if(num==0)

                            {

                                          System.out.println(‘0‘);

                            }

                           

                            while(num!=0)

                            {

                                          inttemp = num&base;

                                         

                                          arr[post++]= chs[temp];

 

                            num= num>>>bit;

                                         

                            }

             

                            for(int x=post-1;x>=0 ;x--)//post在上面的循环中已经是最大值,这里就必须使用--.post的位置打印。

                            {

                                          System.out.print(arr[x]);

                            }

 

                            System.out.println();

             

              }

             

}

二维数组

二维数组本质上是以数组作为数组元素的数组,即“数组的数组”。

格式:

int [] []arr =new arr [4] [5]

int [] []arr =new arr{{1,2,3,4},{5,6,7,8,9}}

示例1、定义一个二维数组遍历并打印元素内容的方法。

思路:

1、  因为是二维数组的遍历,肯定要用到for嵌套循环,外循环确定小数组的个数,内循环确定小数组内的元素。

2、  就像打印矩形一样(也可以理解成表格),外循环是行数,内循环是列数。

class sz3

{

              publicstatic void main(String[] args)

              {

                            int[] [] arr  ={{1,2,3,4},{5,6,7,8},{9,10,11,12,13},{14,15,16,17,18}};

 

                            bl(arr);

 

              }

 

              publicstatic void bl (int [] [] arr)

              {

                            for(int x=0;x<arr.length ;x++ )//定义for循环,x应该是二维数组中的小数组个数。

                            {                                                                    

                                                        System.out.print(x+"数组");

                                                        for(int y=0;y<arr[x].length ;y++ )//定义一个内循环,来遍历小数组中的元素,arr[x]指的是小数组的元素角标。

                                                        {

                                                                     

                                                                      System.out.print(arr[x][y]+"");

                                                        }

                                                                     

                                                                      System.out.println();                               

                            }            

              }

}

 

 

  • 方法(函数)

概念:方法就是一段可以重复调用的代码段,在Java中如果某些代码希望可以被重复调用的时候,就可以使用方法进行定义,但是在定义的时候,因为现在要求所有的方法是可以由主方法直接调用的,所以,此时的方法声明格式如下:

public static 返回值类型 方法名称 (参数列表)

{

return 返回值;

}

 

在定义方法的时候方法名称的书写要求,第一个单词的首字母小写,之后的每个单词首字母大写

示例 1

public class TestDemo

{

public static void main(String args [] )

{

                            testPrint(19);

}


public static voidtestPrint (int num)//定义了一个打印数据的方法。

{

                            System.out.println(num);

}

 }


示例2、当方法指定了返回值类型,就必须使用return返回。(void表示没有具体返回值类型)
class  tes1

{

      publicstatic void main(String[] args)

      {                          

                    intx = add(10,20);

                    System.out.println(x);              

      }

      publicstatic int add (int x , int y)

      {

                    inttemp =x+y;

                    returntemp;   

      }

}

使用void声明的时候也可以使用return返回值。



方法的重载:

方法名称相同,但参数列表不同。当返回值类型不同时,并不是函数的重载。

class tes1

{

      publicstatic void main(String[] args)

      {                          

                    intx = add(10,20);


                    inty = add (10,20,30);

                    System.out.println(y);              

      }


      publicstatic int add (int x , int y)

      {

                    inttemp =x+y;

                    returntemp;   

      }


      publicstatic int add(int x, int y, int z)

      {

                    inttemp = x+y+z;

                    returntemp;

      }

}


方法的递归

指的是一个方法不段的调用自己。

示例:

class tes1

{

      publicstatic void main(String[] args)

      {                          

                    intnum=0;

                    for(int x=0;x<=100 ;x++ )

                    {

                                  num+=x;

                    }

                    System.out.println(num);

      }

}

关于方法的调用,可以参考上面数组中的代码。


Java基础总结2