欢迎访问宙启技术站
智能推送

常见Java函数的示例代码

发布时间:2023-05-29 02:26:55

1. Java里计算两个整数相加的函数。

public int add(int a, int b) {
  return a + b;
}

2. Java里计算两个浮点数相加的函数。

public double add(double a, double b) {
  return a + b;
}

3. Java里计算两个整数的除法并返回结果的函数。

public int divide(int a, int b) {
  if (b == 0) {
    throw new IllegalArgumentException("Cannot divide by zero");
  }
  return a / b;
}

4. Java里计算一个整数的阶乘的函数。

public int factorial(int n) {
  if (n < 0) {
    throw new IllegalArgumentException("n must be non-negative");
  }
  if (n == 0 || n == 1) {
    return 1;
  }
  return n * factorial(n-1);
}

5. Java里计算一个整数的平方的函数。

public int square(int n) {
  return n * n;
}

6. Java里将一个字符串转换成小写字母的函数。

public String toLowerCase(String s) {
  return s.toLowerCase();
}

7. Java里将一个字符串转换成大写字母的函数。

public String toUpperCase(String s) {
  return s.toUpperCase();
}

8. Java里返回一个数组中最大值的函数。

public int getMax(int[] arr) {
  if (arr == null || arr.length == 0) {
    throw new IllegalArgumentException("Array must not be empty");
  }
  int max = arr[0];
  for (int i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
      max = arr[i];
    }
  }
  return max;
}

9. Java里返回一个数组中最小值的函数。

public int getMin(int[] arr) {
  if (arr == null || arr.length == 0) {
    throw new IllegalArgumentException("Array must not be empty");
  }
  int min = arr[0];
  for (int i = 1; i < arr.length; i++) {
    if (arr[i] < min) {
      min = arr[i];
    }
  }
  return min;
}

10. Java里将一个数组中的元素顺序颠倒的函数。

public void reverseArray(int[] arr) {
  if (arr == null || arr.length == 0) {
    throw new IllegalArgumentException("Array must not be empty");
  }
  int left = 0;
  int right = arr.length - 1;
  while (left < right) {
    int temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;
    left++;
    right--;
  }
}

11. Java里判断一个字符串是否是回文的函数(即从头到尾和从尾到头读都一样)。

public boolean isPalindrome(String s) {
  if (s == null || s.length() == 0) {
    throw new IllegalArgumentException("String must not be empty");
  }
  int left = 0;
  int right = s.length() - 1;
  while (left < right) {
    if (s.charAt(left) != s.charAt(right)) {
      return false;
    }
    left++;
    right--;
  }
  return true;
}

12. Java里判断一个整数是否是质数的函数。

public boolean isPrime(int n) {
  if (n < 2) {
    return false;
  }
  for (int i = 2; i <= Math.sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

13. Java里计算一个double类型的数的绝对值的函数。

public double abs(double d) {
  return d < 0 ? -d : d;
}

14. Java里计算一个整数的绝对值的函数。

public int abs(int n) {
  return n < 0 ? -n : n;
}

15. Java里计算一个整数的平方根的函数。

public double sqrt(int n) {
  if (n < 0) {
    throw new IllegalArgumentException("n must be non-negative");
  }
  return Math.sqrt(n);
}

16. Java里计算一个double类型的数的开方的函数。

public double sqrt(double d) {
  if (d < 0) {
    throw new IllegalArgumentException("d must be non-negative");
  }
  return Math.sqrt(d);
}

17. Java里判断一个字符串是否是数字的函数。

public boolean isNumeric(String s) {
  if (s == null || s.length() == 0) {
    throw new IllegalArgumentException("String must not be empty");
  }
  for (int i = 0; i < s.length(); i++) {
    if (!Character.isDigit(s.charAt(i))) {
      return false;
    }
  }
  return true;
}

18. Java里将一个字符串按照指定的分隔符分割成多个字符串的函数。

public String[] split(String s, String delimiter) {
  if (s == null || s.length() == 0) {
    throw new IllegalArgumentException("String must not be empty");
  }
  if (delimiter == null || delimiter.length() == 0) {
    throw new IllegalArgumentException("Delimiter must not be empty");
  }
  return s.split(delimiter);
}

19. Java里从一个数组中随机选取一个元素的函数。

public int getRandom(int[] arr) {
  if (arr == null || arr.length == 0) {
    throw new IllegalArgumentException("Array must not be empty");
  }
  int index = (int) (Math.random() * arr.length);
  return arr[index];
}

20. Java里将一个数组转换成字符串的函数。

public String arrayToString(int[] arr) {
  if (arr == null || arr.length == 0) {
    throw new IllegalArgumentException("Array must not be empty");
  }
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < arr.length; i++) {
    sb.append(arr[i]);
    if (i < arr.length - 1) {
      sb.append(",");
    }
  }
  return sb.toString();
}