java基础复习纲要(1)

Java


第4章 数学函数、字符和字符串

4.2 常用数学函数

4.2.1 三角函数方法

Math类中的三角函数方法

方法 描述
sin(radians) 返回以弧度为单位的角度的三角正弦函数值
cos(radians) 返回以弧度为单位的角度的三角余弦函数值
tan(radians) 返回以弧度为单位的角度的三角正切函数值
toRadians(degree) 将以度为单位的角度值转换为以弧度表示
toDegrees(radians) 将以弧度为单位的角度值转换为以度表示
asin(a) 返回以弧度为单位的角度的反三角正弦函数值
acos(a) 返回以弧度为单位的角度的反三角余弦函数值
atan(a) 返回以弧度为单位的角度的反三角正切函数值

4.2.2 指数函数方法

Math类中的指数函数方法

方法 描述
exp(x) 返回e的x次方
log(x) 返回x的自然底数
log10(x) 返回x的以10为底的对数
pow(a,b) 返回a的b次方
sqrt(x) 对于x>=0的数字

4.2.3 取整方法

Math类中的取整方法

方法 描述
ceil(x) x向上取整为它最接近的整数。该整数作为一个双精度值返回
floor(x) x向下取整为它最接近的整数。该整数作为一个双精度值返回
rint(x) x取整为它最接近的整数。如果x与两个整数的距离相等,偶数的整数作为一个双精度值返回
round(x) 如果x是单精度数, 返回(int)Math.floor(x+0.5);如果x是双精度数, 返回(long)Math.floor(x+0.5)

4.3 字符数据类型操作

4.3.2 特殊字符的转义序列

转义序列

转义序列 名称 Unicode码 十进制值
\b 退格键 \u0008 8
\t Tab键 \u0009 9
\n 换行符 \u000A 10
\f 换页符 \u000C 12
\r 回车符 \u000D 13
\\ 反斜杠 \u005C 92
\” 双引号 \u0022 34

4.3.3 字符型数据与数值型数据之间的转换

char ch = (char)0XAB0041; //整数转换成char型数据
char ch = (char)65.25; //浮点型转换成char型数据
int i = (int)`A`; //char型数据转换成数值型数据

4.3.4 字符的比较和测试

Character类中的方法

方法 描述
isDigit(ch) 如果指定的字符是一个数字,返回true
isLetter(ch) 如果指定的字符是一个字母,返回true
isLetterOrDigit(ch) 如果指定的字符是一个字母或者数字,返回true
isLowerCase(ch) 如果指定的字符是一个小写字母,返回true
isUpperCase(ch) 如果指定的字符是一个大写字母,返回true
toLowerCase(ch) 返回指定字符的小写形式
toUpperCase(ch) 返回指定字符的大写形式

4.4 String类型

String对象的简单方法

方法 描述
length() 返回字符串中的字符
charAt(index) 返回字符串s中指定位置的字符
concat(s1) 将本字符串和字符串s1连接,返回一个新字符串
toUpperCase() 返回一个新字符串,其中所有的字母大写
toLowerCase() 返回一个新字符串,其中所有的字母小写
trim() 返回一个新字符串,去掉了两边的空白字符

4.4.7 字符串比较

String对象的比较方法

方法 描述
equals(s1) 如果该字符串等于字符串s1,返回true
equalsIgnoreCase(s1) 如果该字符串等于字符串s1,返回true;不区分大小写
compareTo(s1) 返回一个大于、小于或等于0的整数,表明一个字符串是否大于、小于或者等于s1
compareToIgnoreCase(s1) 和compareTo一样,不区分大小写
startsWith(prefix) 如果字符串以特定的前缀开始,返回true
endWith(suffix) 如果字符串以特定的后缀结束,返回true
contains(s1) 如果s1是该字符串的子字符串,返回true

4.4.8 获得子字符串

String类包含的获取子串的方法

方法 描述
substring(beginIndex) 返回该字符串的子串,从特定位置beginIndex的字符开始到字符串结尾
substring(beginIndex,endIndex) 返回该字符串的子串,从特定位置beginIndex的字符开始到下标为endIndex-1的字符

4.4.9 获取字符串中的字符或者子串

String类包含获取子串的方法

方法 描述
indexOf(ch) 返回字符串中出现的第一个ch的下标。如果没有匹配的,返回 -1
indexOf(ch, fromIndex) 返回字符串中fromIndex之后出现的第一个ch的下标。如果没有匹配的,返回 -1
indexOf(s) 返回字符串中出现的第一个字符串s的下标。如果没有匹配的,返回 -1
indexOf(s, fromIndex) 返回字符串中fromIndex之后出现的第一个字符串s的下标。如果没有匹配的,返回 -1
lastIndexOf(ch) 返回字符串中出现的最后一个ch的下标。如果没有匹配的,返回 -1
lastIndexOf(ch, fromIndex) 返回字符串中fromIndex之前出现的最后一个ch的下标。如果没有匹配的,返回 -1
lastIndexOf(s) 返回字符串中出现的最后一个字符串s的下标。如果没有匹配的,返回 -1
lastIndexOf(s, fromIndex) 返回字符串中fromIndex之前出现的最后一个字符串s的下标。如果没有匹配的,返回 -1

4.4.10 字符串和数字之间的转换

int intValue = Integer.parseInt(intString); //字符串转换为数值
double doubleValue  = double.parseInt(doubleString); //字符串转换为double值
String s = number + ""; //数值转换为字符串

第7章 一维数组

7.2 数组的基础知识

7.2.1 声明数组变量

  • elementType[] arrayRefVar;

    (元素类型[] 数组引用变量;)

7.2.2 创建数组

  • elementType[] arrayRefVar = new elementType[arraySize];

    (元素类型[] 数组引用变量 = new 元素类型[数组大小];)

  • elementType arrayRefVar[] = new elementType[arraySize];

    (元素类型 数组引用变量[] = new 元素类型[数组大小];)

7.2.5 数组初始化语法

  • elementType[] arrayRefVar = {value0, value1, ···, valuek};

    (元素类型[] 数组引用变量 = {值0,值1,···,值k};)

7.2.6 处理数组

  1. (使用输入值初始化数组)

    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print("Enter " + myList.length + " values");
    for(int i = 0; i < myList.length; i++)
       myList[i] = input.nextDouble();
  2. (使用随机数组初始化数组)

    for(int i = 0; i < myList.length; i++) {
       myList[i] = Math.radom() * 100;
    }
  3. (显示数组)

    for(int i= 0; i < myList.length; i++) {
       System.out.print(myList[i] + " ");
    }
    //char[]类型的数组
    char[] city = {'D', 'a', 'l', 'l', 'a', 's'};
    System.out.println(city);
  4. (对所有元素求和)

    double total = 0;
    for(int i = 1; i < myList.length; i++) {
       total += myList[i];
    }
  5. (找出最大元素)

    double max = myList[i];
    for(int i = 0; i < myList.length; i++) {
       if(myList[i] > max) max = myList[i];
    }
  6. (找出最大元素的最小下标值)

    double max = myList[0];
    int indexOfMax = 0;
    for(int i = 0; i < myList.length; i++) {
       if(myList[i > max]) {
           max = myList[i];
           indexOfMax = i;
       }
    }
  7. (随机乱打)

    for(int i = myList.length - 1; i > 0; i--) {
       int j = (int)(Math.radom) * (i + 1));
       double temp = myList[i];
       myList[i] = myList[i];
       myList[i] = temp;
    }
  8. (移动元素)

    double temp = myList[0];
    for(int i = 1; i < myList.length; i++) {
       myList[i - 1] = myList[i];
    }
    myList[myList.length - 1] = temp;

7.2.7 foreach循环

for(elementType element: arrayRefVar) {
     //Process the element
}

7.5 数组的复制

  • 复制数组的三种方法:

    1. 使用循环语句逐个的复制数组的元素

      int[] sourceArray = {2, 3, 1, 5, 10};
      int[] targetArray = new int[sourceArray.length];
      for(int i = 0; i < sourceArray.length; i++) {
        targetArray[i] = sourceArray[i];
      }
    2. 使用System类中的静态方法arraycopy

      //使用java.lang.System类中的arraycopy方法复制
      System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
      • 注:arraycopy方法违反了Java命名习惯。根据命名习惯,该方法应该命名为arrayCopy
    3. 使用clone方法复制数组

7.9 可变长参数列表

  • typeName... parameterName (类型名… 参数名)

    • java
      public class VarArgsDemo {
      public static void main(String[] args) {
      printMax(34, 3, 3, 2 ,56.5);
      printMax(new double[]{1, 2, 3});
      }
      public static void printMax(double... numbers) {
      if(numbers.length == 0) {
      System.out.println("No argument passed");
      return ;
      }
      double result = number[0];
      for(int i = 1; i < numbers.length; i++)
      if(numbers[i] > result)
      result = numbers[i];
      System.out.println("The max value is " + result);
      }
      }

7.10 数组的查找

  • 线性查找二分查找

7.10.1 线性查找法

public class LinearSearch {
    public static int linearSearch(int[] list, int key) {
        for(int i = 0; i < list.length; i++) {
            if(key == list[i])
                return i;
        }
        return -1;
    }
}

7.10.2 二分查找法

public static int binarySearch(int[] list, int key) {
    int low = 0;
    int high = list.length - 1;
    while(high >= low) {
        int mid = (low + high) / 2;
        if(key < list[mid])
            high = mid - 1;
        else if(key == list[mid])
            return mid;
        else
            low = mid + 1;
    }
    return -low - 1;
}
* 注:使用二分查找法时数组中元素必须已经排好序(上面数组已按升序排列)。

7.11 数组的排序

public class SelectionSort {
    public static void selectionSort(double[] list) {
        for(int i = 0; i < list.length - 1; i++) {
            double currentMin = list[i];
            int currentMinIndex = i;
            for(int j = i - 1; j < list.length; j++) {
                if(currentMin > list[j]) {
                    currentMin = list[j];
                    currentMinIndex = j;
                }
            }      
            if(currentMinIndex != i) {
                list[currentMinIndex] = list[i];
                list[i] = currentMin;
            }
        }
    }
}

7.12 Arrays类

  1. 采用sort或者paralleSort方法对整个数组或者部分数组进行排序:

    //对整个数组进行排序
    double[] numbers = {value0, value1, ..., valuek};
    java.util.sort(numbers); //按升序排列
    java.util.parallelSort(numbers); //按降序排列
    //对部分数组进行排序
    char[] chars = {'ch1', 'ch2', ..., 'chk'};
    java.util.sort(chars, 1, 3); //按升序排列
    java.util.parallelSort(chars, 1, 3); //按降序排列
    //此时是从chars[1]到chars[3-1]的部分数组排序
  2. 采用二分查找法(binarySearch方法)在数组中查找关键字:

    int[] list = {value0, value1, ..., valuek};
    System.out.println("Index is " + java.util.Arrays.binarySearch(list, number));
    
    char[] chars = {'ch1', 'ch2', ..., 'chk'};
    System.out.println("Index is " + java.util.Arrays.binarySearch(chars, 'ch'));
  3. 采用equals方法检测两个数组是否相等:

    int[] list1 = {2, 4, 7, 10};
    int[] list2 = {2, 4, 7, 10};
    int[] list3 = {4, 2, 7, 10};
    System.out.println(java.util.Arrays.equals(list1, list2)); //true
    System.out.println(java.util.Arrays.equals(list2, list3)); //false
  4. 采用fill方法填充整个数组或部分数组:

    int[] list1 = {2, 4, 7, 10};
    int[] list2 = {2, 4, 7, 7, 7, 10};
    java.util.Arrays.fill(list1, 5); //将5填充到list1中
    java.util.Arrays.fill(list2, 1, 5, 8); //将8填充到元素list2[1]到list2[5-1]中
  5. 采用toString方法来返回一个字符串:

    int[] list = {2, 4, 7, 10};
    System.out.println(java.util.Arrays.toString(list));

第8章 多维数组

8.2 二维数组的基础知识

8.2.1 声明二维数组变量并创建二维数组

  • elementType[][] arrayRefvar; //(数据类型[][] 数组名)
  • elementType arrayRefvar[][]; //(数据类型 数组名[][])
  • elementType[][] arrayRefvar = new elementType[i][j];

8.3 处理二维数组

  1. (使用输入值初始化数组)

    java.Scanner input = new Scanner(System.in);
    System.out.println("Enter " + matrix.length + " rows and" + 
                    matrix[0].length + "columns: ");
    for(int row = 0; row < matrix.length; row++) {
       for(int column = 0; column < matrix[0].length; column++) {
           matrix[row][column] = input.nextInt();
       }
    }
  2. (使用随机值初始化数组)

    for(int row = 0; row < matrix.length; row++) {
       for(int column = 0; column < matrix[0].length; column++) {
           matrix[row][column] = (int)(Math.random() * 100);
       }
    }
  3. (打印数组)

    for(int row = 0; row < matrix.length; row++) {
       for(int column = 0; column < matrix[0].length; column++) {
           System.out.print(matrix[row][column] + " ");
       }
    }
  4. (求所有元素的和)

    int total = 0;
    for(int row = 0; row < matrix.length; row++) {
       for(int column = 0; column < matrix[0].length; column++) {
           total += matrix[row][column];
       }
    }
  5. (对数组按列求和)

    for(int column = 0; column < matrix[0].length; column++) {
       int total = 0;
       for(int row = 0; row < matrix.length; row++) {
           total += matrix[row][column];
           System.out.println("Sum of column " + column + " is " + total);
       }
    }
  6. (哪一行的和最大)

    int maxRow = 0;
    int indexOfMaxRow = 0;
    for(int column = 0; column < matrix[0].length; column++) {
       maxRow = matrix[0][column];
    }
    for(int row = 0; row < matrix.length; row++) {
       int totalOfThisRow = 0;
       for(int column = 0; column < matrix[0].length; column++)
           totalOfThisRow += matrix[row][column];
       if(totalOfThisRow > maxRow) {
           maxRow = totalOfThisRow;
           indexOfMaxRow = row;
       }
    }
    System.out.println("Row " + indexOfMaxRow + " has the maximum sum of " + maxRow);
  7. (随意乱打)

    for(int i = 0; i < matrix.length; i++) {
       for(int j = 0; j < matrix[i].length; j++) {
           int i1 = (int)(Math.random() * matrix.length);
           int j1 = (int)(Math.random() * matrix[i].length);
           int temp = matrix[i][j];
           matrix[i][j] = matrix[i1][j1];
           matrix[i1][j1] = temp;
       }
    }

8.4 将二维数组传递给方法

import java.util.Scanner;

public class arrays {
    public static void main(String[] args) {
        int[][] m = getArray();
        System.out.println("\nSum of all elements is " + sum(m));
    }

    //返回一个二维数组
    public static int[][] getArray() {
        Scanner input = new Scanner(System.in);
        int[][] = new int[3][4];
        System.out.println("Enter " + m.length + " rows and " +
                          m[0].length + " columns: ");
        for(int i = 0; i < m.length; i++)
            for(int j = 0; j < m[i].length; j++)
                m[i][j] = input.nextInt();
        return m;
    }

    //返回一个矩阵中所有元素的和
    public static int sum(int[][] m) {
        int total = 0;
        for(int row = 0; row < m.length; row++) {
            for(int column = 0; column < m[row].length; column++) {
                total += m[row][column];
            }
        }
        return total;
    }
}

8.8 多维数组

  • elementType[][][] arrayRefvar = new elementType[i][j][k]; //三维数组

猜你喜欢

转载自blog.csdn.net/Heimdall_vata/article/details/80861695