
com.github.sinjar.common.util.common.ArrUtils Maven / Gradle / Ivy
package com.github.sinjar.common.util.common;
import java.util.Arrays;
/**
* Description :
*
* @author CPF
* Date: 2020/3/27 14:42
*/
public class ArrUtils {
private ArrUtils(){}
/**
* 获取一维数组中相邻 number 个数的最大乘积
*
* @param arr 待处理的数组
* @param number 相邻数量
* @return
* throw new RuntimeException (arr.length < number)
*/
public static int getMaxProductInArr(int[] arr, int number) throws Throwable {
int len = arr.length;
if (len < number){
throw new RuntimeException("arr太小!");
}
int[] num = Arrays.copyOf(arr, number);
int product = Arrays.stream(num).reduce((a, b) -> a*b).orElseThrow(Throwable::new);
int cur = 0;
int tmpProdect;
for (int i = number; i < len; i ++) {
num[cur] = arr[i];
tmpProdect = Arrays.stream(num).reduce((a, b) -> a*b).orElseThrow(Throwable::new);
if (tmpProdect > product) {
product = tmpProdect;
}
cur ++;
cur = cur % number;
}
return product;
}
/**
* 转置矩阵, 矩阵必须是方形矩阵
* @param matrix 矩阵数组
*/
public static void transposeMatrix(Object[][] matrix){
if (matrix.length != matrix[0].length){
throw new RuntimeException("matrix.length != matrix[0].length");
}
int length = matrix.length;
Object tmp;
for (int i = 0; i < length; i ++){
for (int j = 0; j < i; j++){
tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
}
/**
* 转置矩阵
*
* @param matrix 矩阵数组
*/
public static void transposeMatrix(int[][] matrix){
if (matrix.length != matrix[0].length){
throw new RuntimeException("matrix.length != matrix[0].length");
}
int length = matrix.length;
int tmp;
for (int i = 0; i < length; i ++){
for (int j = 0; j < i; j++){
tmp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = tmp;
}
}
}
/**
* 将一个字符串数组转换为int数组
* @param strArr 待处理的字符串数组
* @return 转换后的int数组
*/
public static int[] transStrArrToIntArr(String[] strArr){
int len = strArr.length;
int[] intArr = new int[len];
for (int i = 0; i < len; i++){
intArr[i] = Integer.parseInt(strArr[i]);
}
return intArr;
}
/**
* 全层数组深拷贝,
* @param arr 拷贝后的数组
*/
@SuppressWarnings("unchecked")
public static T[] fullClone(T[] arr){
int len = arr.length;
T[] copy = arr.clone();
for (int i = 0; i 类型
*/
public static void fullFill(T[] arr, T val) {
int len = arr.length;
for (int i = 0; i end: %s 间隔不能为 : %s", start, end, interval));
}
if (len == 0) {
int[] arr = new int[1];
arr[0] = start;
return arr;
}
int[] result = new int[len];
for (int i = 0, n = start; i < len; i++, n += interval) {
result[i] = n;
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy