可以。
int i = 7;
int[] array = new int[i];
应用:杨辉三角
public static void main(String[] args) {
int [][]arr = new int[10][];
for (int i = 0; i < arr.length; i++){ // kong
arr[i] = new int[i + 1];
for (int j = 0; j < arr[i].length; j++){ //lei
if (j == 0 || i == j) {
arr[i][j] = 1;
}else{
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
操作二维数组的注意点:
操作二维数组不应使用常数来控制维数。具体方法是array.length表示行数,array[row].length来表示row行的列数。这样当数组行数和列数不相等时,代码可以自动调整为正确的值。
数组没有length()方法,但是length属性;
String有length()方法,但是没有length属性
是的。
return getClass().getName() + "@" + Integer.toHexString(hashCode());
public class Test_stack {
public static int[] generateRomdom(int length, int rangeL, int rangeR){
if (length <= 0 || rangeL > rangeR){
return null;
}
return new Random().ints(rangeL, rangeR).limit(length).toArray();
}
public static void swap(int[] arr, int curr, int index){
int tmp = arr[curr];
arr[curr] = arr[index];
arr[index] = tmp;
}
public static void move(int []arr, int m){
if (m >= arr.length){
System.out.println("最多只能移动[" + (arr.length - 1) + "]位");
return;
}
int[] tmp = new int[m];
for (int i = 0; i < m; i++){
tmp[i] = arr[arr.length - m + i];
}
// for (int i = 0 ; i + m < arr.length; i++) // 不要从前往后填充,因为会被替换掉
for (int i = arr.length - m - 1; i > -1; i--){
arr[i + m] = arr[i];
}
for (int i = 0; i < tmp.length; i++){
arr[i] = tmp[i];
}
}
public static void main(String[] args){
int[] arr = generateRomdom(10, 10, 100);
assert arr != null;
System.out.println(Arrays.toString(arr));
move(arr, 4);
System.out.println(Arrays.toString(arr));
}
}
package com.company;
import java.lang.reflect.Array;
import java.util.*;
public class Test_stack {
public static int[] generateRomdom(int length, int rangeL, int rangeR){
if (length <= 0 || rangeL > rangeR){
return null;
}
return new Random().ints(rangeL, rangeR).limit(length).toArray();
}
public static void swap(int[] arr, int curr, int index){
int tmp = arr[curr];
arr[curr] = arr[index];
arr[index] = tmp;
}
public static void main(String[] args){
int[] arr = generateRomdom(10, 10, 100);
assert arr != null;
int max = 0;
int min = 0;
for (int i = 1; i < arr.length; i++){
if (arr[i] > arr[max]){
max = i;
}
if (arr[i] < arr[min]){
min = i;
}
}
System.out.println(Arrays.toString(arr));
swap(arr, max, 0);
swap(arr, min, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
}
public class Test_stack {
public static void reverseArray(int [] arr){
if (arr == null || arr.length < 2){
return;
}
for (int i = 0; i < arr.length / 2; i++){
int temp = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = arr[i];
arr[i] = temp;
}
}
public static void ReversalArr(int[] arr){
if (arr == null || arr.length < 2){
return;
}
for (int i = 0, j = arr.length - 1; i < j; i++, j--){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
public static void main(String[] args){
int[] a = new int[] { (int) (Math.random() * 1000),
(int) (Math.random() * 1000), (int) (Math.random() * 1000),
(int) (Math.random() * 1000), (int) (Math.random() * 1000) };
System.out.println(Arrays.toString(a));
reverseArray(a);
System.out.println(Arrays.toString(a));
}
}
创建一个长度为6的int型数组,要求取值为1-30,同时元素值各不相同
public static void main(String[] args) {
int[] arr = new int[6];
HashSet h = new HashSet<Integer>();
for (int i = 0; i < arr.length;){
int random = new Random().nextInt((30 - 1 + 1) + 1);
if (!h.contains(random)){
arr[i] = random;
h.add(random);
i++;
}
}
for (int i :arr) {
System.out.print(i + "\t");
}
}
public static void main(String[] args) {
int[] arr = new int[6];
for (int i = 0; i < arr.length;){
int random = new Random().nextInt((30 - 1 + 1) + 1);
boolean flag = false;
for (int j = 0; j < i; j++){
if (random == arr[j]){
flag = true;
continue;
}
}
if (flag != true){ // 有相等的
arr[i] = random;
i++;
}
}
for (int i :arr) {
System.out.print(i + "\t");
}
}
public class Test_stack {
public static void main(String[] args){
int [] arr = new int[20];
arr[0] = 0;
arr[1] = 1;
for (int i = 2; i < arr.length; i++){
arr[i] = arr[i-2] + arr[i-1];
}
System.out.println(Arrays.toString(arr));
}
}
从键盘输入一个整数(1~20)
则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。例如: 输入数字2,则程序输出: 1 2
4 3
输入数字3,则程序输出: 1 2 3
8 9 4
7 6 5
输入数字4, 则程序输出:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
方法一:
package com.company;
import java.util.*;
public class Test_stack {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("输入一个行:");
int col = scanner.nextInt();
System.out.println("输入一个列:");
int row = scanner.nextInt();
int [][] arr = new int[col][row];
int count = 0;
int i = 0;
int j = 0; // i和j是要填充下一个元素的索引位置
int sum = col * row; //一共要遍历多少次
while (count < sum){ // 因为col和row每次都会减少两个元素
for (int k = 1; k < row; k++){
arr[i][j] = count + 1;
j++;
count++;
} // k = 4 时, j = 3, 然后 就j = 4
for (int k = 1; k < col; k++){ // 右边除了最下面的元素
arr[i][j] = count + 1;
i++;
count++;
}
for (int k = 1; k < row; k++){ // 下面除了最左边的元素
arr[i][j] = count + 1;
j--;
count++; //i = col - 1 保持不变
}
for (int k = 1; k < col; k++){ // 左边除了最上面的元素
arr[i][j] = count + 1;
i--;
count++; //i = col - 1 保持不变
}
i++; // 起始位置前进
j++;
col = col - 2; ////每次遍历一环横竖各少两个边,所以减去
row = row - 2;
}
for (int a = 0; a < arr.length; a++) {
for (int b = 0; b < arr[a].length; b++){
System.out.print(arr[a][b] + "\t");
}
System.out.println();
}
//System.out.println(Arrays.deepToString(arr));;
}
}
参考
1、创建一个char类型的26个元素的数组,分别 放置’A’-‘Z’。
使用for循环访问所有元素并打印出来。
public class Test_stack {
public static void main(String[] args){
char[] arr = new char[26];
int j = 0;
for (char i = 'A'; i <= 'Z'; i++){
arr[j++] = i;
}
for (char c: arr) {
System.out.printf("%c\t", c);
}
System.out.println();
}
}
2、创建一个char类型的36个元素的数组,前26个元素放置’A’-‘Z’, 后10个元素放置’0’-‘9’。
使用for循环访问所有元素并打印出来。
提示:char类型数据运算 ‘A’+1 -> ‘B’,‘0’+1 -> ‘1’
public class Test_stack {
public static void main(String[] args){
char[] arr = new char[36];
int j = 0;
for (char i = 'A'; i <= 'Z'; i++){
arr[j++] = i;
}
for (char i = '0'; i <= '9'; i++){
arr[j++] = i;
}
for (char c: arr) {
System.out.printf("%c\t", c);
}
System.out.println();
}
}
练习:
1、从键盘输入本组学员的成绩,放到数组中
2、用for循环显示所有学员的成绩
3、排序:从低到高
4、查找是否有正好60分的,如果有返回位置
5、复制成绩最低三名构成新数组
6、用工具类打印成绩最低三名成绩
package com.company;
import java.util.*;
public class Test_stack {
public static void main(String[] args){
//1、声明一个数组并创建一个数组
int[] scores = new int[5];
//2、从键盘输入成绩
Scanner input = new Scanner(System.in);
for(int i=0; i<scores.length; i++){
//成绩存在数组的元素中
//为元素赋值
System.out.print("请输入第" + (i+1) + "个学员的成绩:");
scores[i] = input.nextInt();
}
//3、显示成绩
//用foreach显示所有学员的成绩
System.out.println("本组学员的成绩如下:");
for(int s = 0; s < scores.length;s++){
System.out.print(scores[s] + "\t");
}
System.out.println();
//4、排序:从低到高
Arrays.sort(scores);
System.out.println("排序后的结果:" + Arrays.toString(scores));
//5、查找60分
int index = Arrays.binarySearch(scores, 60);
if(index<0){
System.out.println("没有正好60分的");
}else{
System.out.println("60分的索引位置:" + index);
}
//6、复制成绩最低三名构成新数组
//int[] newArray = Arrays.copyOfRange(scores, 0, 3);
int[] newArray = Arrays.copyOf(scores, 3);
//7、用工具类打印成绩最低三名成绩
System.out.println("成绩最低的三名同学是:" + Arrays.toString(newArray));
}
}
/*
0-9 儿童
10-19 少年
20-29 青少年
30-39 青年
40-49 壮年
50-59 中年
60-69 中老年
70-79 老年
80-89 老老年
90-99 老老老年
*/
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年龄:");
int age = scanner.nextInt();
int bage = age / 10;
switch (bage){
case 0:
System.out.println("儿童");
break;
case 1:
System.out.println("少年");
break;
case 2:
System.out.println("青少年");
break;
case 3:
System.out.println("青年");
break;
case 4:
System.out.println("壮年");
break;
case 5:
System.out.println("少年");
break;
case 6:
System.out.println("青少年");
break;
case 7:
System.out.println("青年");
break;
case 8:
System.out.println("老老年");
break;
case 9:
System.out.println("老老老年");
break;
default:
System.out.println("啦啦啦啦啦老");
break;
}
}
斐波那契数列(Fibonacci sequence),又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递推的方法定义:F(1)=1,F(2)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 3,n ∈ N*)
public class Test_stack {
public static void main(String[] args){
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
for (int i = 2; i < arr.length; i++){
arr[i] = arr[i-1] + arr[i-2];
}
for (int i: arr) {
System.out.printf("%d\t", i);
}
}
}
https://blog.csdn.net/blue_hh/article/details/75453603
package com.company;
import java.util.*;
public class Test_stack {
public static int[] generateRomdom(int length, int rangeL, int rangeR){
if (length <= 0 || rangeL > rangeR){
return null;
}
return new Random().ints(rangeL, rangeR).limit(length).toArray();
}
// 二分查找法
// 返回值: 要么是因为找到了,返回找到的索引
// 要么是因为找不到,但是由于 low > high. 因而返回 -(low + 1)
public static int halfSearch(int[] arr, int element, int from, int to){
if (arr == null || from > to){
return -1;
}
int low = from;
int high = to - 1;
while (low <= high){
int mid = low + (high - low) / 2;
if (arr[mid] < element){
low = mid + 1;
}else if (arr[mid] > element){
high = mid - 1;
}else {
return mid; // 找得到就绝对不是负数
}
}
/*
* 当没有找到要查找的关键字时,-(-low-1)就是一个插入点,这个位置插入关键字可以保持序列的有序性。
* 解释:首先明确一下,一定要返回一个负值,表示关键值不在序列里面。
* 但可不可以返回-low?
答:不可以,如果关键字小于low[0],那么-0也是0,这表明关键字与list[0]匹配。
* 所以一个好的选择是返回-low-1,不仅说明了关键字不在序列,也指出了它应该插在哪个位置。
* */
return -(low + 1); // 找不到就是负数
}
// arr是一个已经排好序的数组
public static int[] insertElement(int[] original, int element, int index){ //要插入的索引位置
int length = original.length;
int destination[] = new int[length + 1];
System.arraycopy(original, 0, destination, 0, index );
// System.out.println(Arrays.toString(destination) + " ---------------" + index);
destination[index] = element;
System.arraycopy(original, index , destination, index + 1, length - index);
return destination;
}
public static void main(String[] args){
int [] arr = generateRomdom(10, 100, 1000);
System.out.println(Arrays.toString(arr));
assert arr != null;
// Arrays.fill(arr, 1, 2, 500);
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
int ele = 300;
int index = halfSearch(arr, ele, 0, arr.length);
System.out.println("元素" + ele + "存在索引" + index + "处(负数表示不存在)" + arr[-index]);
int newIndex = - index - 1;
int[] brr = insertElement(arr, ele, newIndex);
System.out.println(Arrays.toString(brr));
}
}
public class Test_stack {
// 生成一个长度为n,范围在[rangeL, rangeR]内的随机数组
public static int[] generateRandomArray(int length, int rangeL, int rangeR){
if (length <0 || rangeR <= rangeL) {
return null;
};
return new Random().ints(rangeL, rangeR).limit(length).toArray();
}
public static void swap(int[]arr, int a, int b){
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static void QuickSort(int []arr, int start, int end){
if (arr == null || arr.length < 2 || start >= end){
return;
}
int[] mids = helan(arr, start, end);
QuickSort(arr, start, mids[0] - 1);
QuickSort(arr, mids[1] + 1, end );
}
public static int[] helan(int[] arr, int start, int end){
int less = start - 1;
int more = end + 1;
int pivot = arr[start];
int curr = start;
while (curr < more){
if (arr[curr] < pivot){
swap(arr, ++less, curr++); //less是小于基准元素的索引
}else if (arr[curr] > pivot){
swap(arr, --more, curr); // more是大于基准元素的索引
}else {
curr++;
}
}
return new int[]{less + 1, more - 1}; // less + 1, more - 1 为等于基准元素的边界
}
public static void main(String[] args){
int [] arr = generateRandomArray(10, 0, 5);
System.out.println(Arrays.toString(arr));
QuickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
}
public static int[] generateDistinctRomdom(int length, int rangeL, int rangeR){
if (length <= 0 || (rangeR - rangeL + 1) < length){
return null;
}
return new Random().ints(rangeL, rangeR).distinct().limit(length).toArray();
}
public class Test_stack {
public static int answer_1(int [][]arrts){
if (arrts == null) {
return 0;
}
int sum = 0;
for (int i = 0; i < arrts.length; i++){
sum = arrts[i][i] + sum;
}
return sum;
}
public static void main(String[] args){
int attrs[][] = {
{0 ,1 ,2 ,3 },
{11,12,13,4 },
{10,15,14,5 },
{9 ,8 ,7 ,6 },
};
int sum = answer_1(attrs);
System.out.println(sum); // 32
}
}
public class Test_stack {
public static void main(String[] args){
int attrs[][] = {
{0 ,1 ,2 ,3 },
{11,12,13,4 },
{10,15,14,5 },
{9 ,8 ,7 ,6 },
};
for (int i = 0; i < attrs.length / 2; i++){
for (int j = 0; j < attrs[i].length; j++){
int temp = attrs[i][j];
attrs[i][j] = attrs[attrs.length - 1 - i][j];
attrs[attrs.length - 1 - i][j] = temp;
}
}
for (int i = 0; i < attrs.length; i++) {
for (int j = 0; j < attrs[i].length; j++){
System.out.printf("%d\t", attrs[i][j]);
}
System.out.println();
}
}
}
public static void main(String[] args){
int attrs[][] = {
{0 ,1 ,2 ,3 },
{11,12,13,4 },
{10,15,14,5 },
{9 ,8 ,7 ,6 },
};
int[] arr = new int[attrs.length];
for (int i = 0; i < attrs.length ; i++){
int max = attrs[i][0];
for (int j = 1; j < attrs[i].length; j++){
if (attrs[i][j] > max){
max = attrs[i][j];
}
}
arr[i] = max;
}
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args){
int attrs[][] = {
{0 ,1 ,2 ,3 },
{11,12,13,4 },
{10,15,14,5 },
{9 ,8 ,7 ,6 },
};
for (int i = 0; i < attrs.length ; i++){
for (int j = 0; j < attrs[i].length; j++){
if (i == j){
attrs[i][j] = 0;
continue;
}
}
}
for (int i = 0; i < attrs.length; i++) {
for (int j = 0; j < attrs[i].length; j++){
System.out.printf("%d\t", attrs[i][j]);
}
System.out.println();
}
}
public static void main(String[] args){
int attrs[][] = {
{0 ,1 ,2 ,3 },
{11,12,13,4 },
{10,15,14,5 },
{9 ,8 ,7 ,6 },
};
for (int i = 0; i < attrs.length ; i++){
for (int j = 0; j < attrs[i].length; j++){
if (j > i){
continue;
}
attrs[i][j] = 0;
}
}
for (int i = 0; i < attrs.length; i++) {
for (int j = 0; j < attrs[i].length; j++){
System.out.printf("%d\t", attrs[i][j]);
}
System.out.println();
}
}
public static void main(String[] args){
int attrs[][] = {
{0 ,1 ,2 ,3 },
{11,12,13,4 },
{10,15,14,5 },
{9 ,8 ,7 ,6 },
};
for (int i = 0; i < attrs.length ; i++){
for (int j = 0; j < attrs[i].length; j++){
if (i == 0 || j == 0 || i == (attrs.length - 1 ) || j == (attrs[i].length - 1)){
attrs[i][j] = 0;
}
}
}
for (int i = 0; i < attrs.length; i++) {
for (int j = 0; j < attrs[i].length; j++){
System.out.printf("%d\t", attrs[i][j]);
}
System.out.println();
}
}