public class Main {
public static void main(String[] args) {
String originalString = "hello, world!";
String upperCaseString = convertToUpperCase(originalString);
System.out.println("原始字符串:" + originalString);
System.out.println("转换为大写后的字符串:" + upperCaseString);
}
public static String convertToUpperCase(String str) {
StringBuilder result = new StringBuilder();
for (char c : str.toCharArray()) {
if (Character.isLowerCase(c)) {
result.append(Character.toUpperCase(c));
} else {
result.append(c);
}
}
return result.toString();
}
}
使用类完成
package temp01;
public class Main {
public static void main(String[] args) {
AreaCalculator calculator = new AreaCalculator();
double rectangleArea = calculator.area(5, 4);
System.out.println("矩形面积:" + rectangleArea);
double triangleArea = calculator.area(6, 8);
System.out.println("三角形面积:" + triangleArea);
double circleArea = calculator.area(3);
System.out.println("圆形面积:" + circleArea);
}
}
class AreaCalculator {
// 计算矩形面积
public double area(int length, int width) {
return length * width;
}
// 计算三角形面积
public double area(double base, int height) {
return 0.5 * base * height;
}
// 计算圆形面积
public double area(double radius) {
return Double.parseDouble(String.format("%.2f", Math.PI * radius * radius));
}
}
使用函数直接完成
package temp01;
public class Main {
public static void main(String[] args) {
double rectangleArea = area(5, 4);
System.out.println("矩形面积:" + rectangleArea);
double triangleArea = area(6, 8);
System.out.println("三角形面积:" + triangleArea);
double circleArea = area(3);
System.out.println("圆形面积:" + circleArea);
}
// 计算矩形面积
public static double area(int length, int width) {
return length * width;
}
// 计算三角形面积
public static double area(double base, int height) {
return 0.5 * base * height;
}
// 计算圆形面积
public static double area(double radius) {
return Double.parseDouble(String.format("%.2f", Math.PI * radius * radius));
}
}
package temp01;
public class Main {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
double average = calcAvg(numbers);
System.out.println("数组的平均值为:" + average);
}
public static double calcAvg(int[] array) {
if (array == null || array.length == 0) {
return 0;
}
int sum = 0;
for (int num : array) {
sum += num;
}
return (double) sum / array.length;
}
}
package temp01;
public class Main {
public static void main(String[] args) {
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int[][] new_arr = transposeArr(arr);
for (int[] row : new_arr) {
for (int e : row) {
System.out.print(e + " ");
}
System.out.println();
}
}
public static int[][] transposeArr(int[][] arr) {
int rows = arr.length;
int cols = arr[0].length;
int[][] tArr = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
tArr[j][i] = arr[i][j];
}
}
return tArr;
}
}
改变数据结构的视角:矩阵转置实际上是对矩阵的一种重新组织。原始矩阵的行变成了转置矩阵的列,原始矩阵的列变成了转置矩阵的行。这种转换提供了一种从不同维度观察数据的方式,有助于发现数据在不同方向上的规律和关系。
在数学运算中的便利性:在许多数学计算和理论推导中,转置操作可以简化计算过程。例如,在矩阵乘法中,当计算两个矩阵 和 的乘积 较为复杂时,转置其中一个矩阵(如计算 或 )可能会使计算更容易理解和处理,因为矩阵乘法的规则与行列的排列密切相关。转置操作还在求解线性方程组、计算矩阵的特征值和特征向量等方面发挥重要作用。
数据存储和访问优化:在计算机存储和处理矩阵数据时,转置可以优化数据的访问模式。某些算法对行数据或列数据的访问效率不同,通过转置矩阵,可以根据算法的需求调整数据存储方式,提高算法的运行速度。
原理:在数字图像处理中,图像可以看作是一个二维矩阵,其中每个元素代表图像的一个像素点的亮度或颜色信息。当需要对图像进行旋转操作时(例如,将一幅图像顺时针旋转 90 度),可以通过矩阵转置来实现部分操作。
public class ImageRotator {
public static int[][] rotateImage(int[][] image) {
// 先进行转置
int[][] transposed = transposeMatrix(image);
int rows = transposed.length;
int cols = transposed[0].length;
// 再反转每一行,实现顺时针90度旋转
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols / 2; j++) {
int temp = transposed[i][j];
transposed[i][j] = transposed[i][cols - 1 - j];
transposed[i][cols - 1 - j] = temp;
}
}
return transposed;
}
public static int[][] transposeMatrix(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
int[][] transposedMatrix = new int[cols][rows];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
transposedMatrix[j][i] = matrix[i][j];
}
}
return transposedMatrix;
}
}
Python代码与效果
def rotate_image(image):
# 转置矩阵
transposed = list(map(list, zip(*image)))
rows = len(transposed)
cols = len(transposed[0])
# 反转每一行,实现顺时针 90 度旋转
for i in range(rows):
transposed[i] = transposed[i][::-1]
return transposed
arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
for i in arr:
print(i)
print("-------------------")
arr_s = rotate_image(arr)
for i in arr_s:
print(i)