zoukankan      html  css  js  c++  java
  • java基础语法

    一部分来自java入门到精通,一部分来自java讲义前四章,主要包含数据类型、部分运算符、循环、数组及数组操作等基本语法知识

      先写个小Java过过瘾

    1 public class test{
    2     public static void main(String args[]){
    3         System.out.println("Never Say Hello World");
    4     } 
    5 }

     需要注意的是:

      1、class的名称要与Java文件的命名要一致。

      2、main函数前边的修饰符,public static void

    数据类型:

      1、super 用于引用父类,this 用于引用当前对象

      2、final 用于定义常量,局部唯一, static final 用于定义全局内唯一的常量

      3、关于位运算

     1 // 左移运算符:<<     相当于原数乘以2
     2 // 0011<<1 == 0110
     3 // 右移运算符:>>     相当于原数除以2,正负保持不变,即如果原数为负,前边填充1
     4 // 0011>>1 == 0001
     5 // 无符号右移运算符:>>>     右移一位,无论正负都填充0
     6 // 1111...1111>>>1 == 0111...1111
     7 public class test{
     8     public static void main(String args[]){
     9         int a = 3;
    10         int b = 8;
    11         int c = 8;
    12         int d = -8;
    13         a = a<<1;
    14         b = b>>1;
    15         c = c>>>1;
    16         d = d>>>1;
    17         System.out.println("a<<1 == " + a);
    18         System.out.println("b>>1 == " + b);
    19         System.out.println("c>>>1 == " + c);
    20         System.out.println("d>>>1 == " + d);
    21     } 
    22 }
    23 
    24 // a<<1 == 6
    25 // b>>1 == 4
    26 // c>>>1 == 4
    27 // d>>>1 == 2147483644

       4、三元运算符  

    1 expression?statement_true:statement_false

    循环与数组:

      数组拷贝:

      1、直接赋值,指向的是同一块内存空间

     1 public class test{
     2     public static void main(String args[]){
     3         int[] a = {1,2,3};
     4         int[] b = a;//直接赋值,相当于a和b指向的是同一个数组
     5         
     6         System.out.println("b[1] == " + b[1]);
     7         b[2] = 666;
     8         System.out.println("a[2] == " + a[2]);
     9     } 
    10 }
    11 
    12 //b[1] == 2
    13 //a[2] == 666

      2、System.arraycopy(fromArray, fromIndex, toArray, toIndex, length)

     1 public class main{
     2     public static void main(String args[]){
     3         int[] a = {1,2,3,4,5,6,7};
     4         int[] b = {11,12,13,14,15,16,17};
     5         System.arraycopy(a, 2, b, 3, 4);//将从a[2]开始的连续4个值copy到b[3]开始的4个值中
     6         
     7         for (int i = 0 ; i < b.length ; ++i)
     8             System.out.println("b[" + i + "] == " + b[i]);
     9     } 
    10 }
    11 
    12 b[0] == 11
    13 b[1] == 12
    14 b[2] == 13
    15 b[3] == 3
    16 b[4] == 4
    17 b[5] == 5
    18 b[6] == 6

       不规则数组:Java真是神奇啊,不知道这个在内存中是怎么放的,难道是向哈希表一样存放?

     1 public class test{
     2     public static void main(String args[]){
     3         int[][] a = new int[3][];
     4         a[0] = new int[1];
     5         a[1] = new int[2];
     6         a[2] = new int[3];
     7         for (int i = 0 ; i < a.length ; ++i){
     8             for (int j = 0 ; j < a[i].length ; ++j)
     9                 System.out.print(a[i][j] + "	");//这个“	”啊。。eggache
    10             System.out.println();
    11         }
    12     } 
    13 }
    14 
    15 0
    16 0       0
    17 0       0       0

       

    for-Each

     1 // for-Each
     2 public class test{
     3         public static void main(String args[]){
     4                 int sum = 0;
     5                 int[] nums = {2,4,6,8,10};
     6                 for (int i:nums){
     7                         System.out.print(i + "	");
     8                         sum += i;
     9                 }
    10                 System.out.println("
    Sum of those nums is " + sum);
    11         }
    12 }
    13 
    14 2       4       6       8       10
    15 Sum of those nums is 30
    //for-Each 用于多维数组
    1 for (int x:nums)
    2     for (int y:x)

    switch:

      仅支持byte、short、char、int四种基本类型和String、enum,这六种类型。

    Arrays

      Java8中引入的类,包含一些可以直接对数组进行操作的方法,除了下面介绍的之外还包含一些parallel的并发执行的方法,如parallelSort等,暂不作介绍

    //二分法查找数组范围内是否有key值出现,若数组中不包含key值,返回负数,要求搜索范围内升序排列
    int  binarySearch(type[] a, type key);
    int  binarySearch(type[] a, int fromIndex, int toIndex, type key);
    
    //复制范围内的数组值,超出的弃掉,不足的赋为0或者空等
    type[] copyOf(type[] original, int length);
    type[] copyOfRange(type[] original, int from, int to);
    
    //判断数组长度及值是否相同
    boolean equals(type[] a, type[] a2);
    
    //把范围内数组值赋值为val
    void fill(type[] a, type val);
    void fill(type[] a, int fromIndex, int toIndex, type val);
    
    //排序,默认升序
    void sort(type[] a);
    void sort(type[] a;  int fromIndex, int toIndex);
    
    //将数组转化为字符串,数组间元素用“,”或者空格隔开
    String toString(type[] a);
     1 package test;
     2 import java.util.Arrays;
     3 
     4 public class test{
     5     public static void main(String a[]){
     6         int[] arr = {1,4,7,6,3,2};
     7         Arrays.sort(arr);
     8         for (int i:arr)
     9             System.out.print(i);
    10         System.out.print("
    " + Arrays.toString(arr));
    11     }
    12 }
    13 
    14 // 123467
    15 // [1, 2, 3, 4, 6, 7]
  • 相关阅读:
    洛谷单元最短路标准版——spfa优化
    airline
    有依赖的背包
    挖地雷
    带分数问题
    子集和问题
    循环比赛日程表
    传纸条
    机器分配
    分组背包
  • 原文地址:https://www.cnblogs.com/kbe317/p/5796257.html
Copyright © 2011-2022 走看看