第一章——Java基础(八)——数组进阶
程序员文章站
2022-04-15 11:19:55
我是小康小白,一个平平无奇的Java,Python小白。热爱有趣的文字,生活和远方。个人博客:https://blog.csdn.net/weixin_45791445有问题欢迎QQ联系:1059320343 (记得备注CSDN)个人微信公众号:小康小白一,数组的复制:题目:题目代码:package step1;import java.util.Scanner;public class HelloWorld {public static void main(Strin....
我是小康小白,一个平平无奇的Java,Python小白。热爱有趣的文字,生活和远方。
个人博客:https://blog.csdn.net/weixin_45791445
有问题欢迎QQ联系:1059320343 (记得备注CSDN)
个人微信公众号:小康小白
一,数组的复制:
- 题目:
题目代码:
package step1;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
//动态构建arr1
int[] arr1 = new int[3];
Scanner sc = new Scanner(System.in);
for(int i = 0 ; i< arr1.length ; i++){
arr1[i] = sc.nextInt();
}
/********** Begin **********/
//创建数组arr2
//使用for循环将arr1的数据复制给arr2
//输出arr2
/********** End **********/
}
}
- 答案:
package step1;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
//动态构建arr1
int[] arr1 = new int[3];
Scanner sc = new Scanner(System.in);
for(int i = 0 ; i< arr1.length ; i++){
arr1[i] = sc.nextInt();
}
/********** Begin **********/
//创建数组arr2
int[] arr2 = new int[arr1.length];
//使用for循环将arr1的数据复制给arr2
for(int i=0;i<arr2.length;i++){
arr2[i] = arr1[i];
}
//输出arr2
for(int i=0;i<arr2.length;i++){
System.out.println(arr2[i]);
}
/********** End **********/
}
}
二,数组中元素的查找
- 题目
题目代码:
package step2;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//str为要查找的字符串
String str = sc.next();
/********** Begin **********/
//创建数组 arr 给数组赋值 {"张三","张三丰","张无忌","王二麻子","张富贵"}
/********** End **********/
}
}
- 答案:
package step2;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//str为要查找的字符串
String str = sc.next();
/********** Begin **********/
//创建数组 arr 给数组赋值 {"张三","张三丰","张无忌","王二麻子","张富贵"}
String[] arr = new String[]{"张三","张三丰","张无忌","王二麻子","张富贵"};
for(int i = 0;i<arr.length;i++){
if(str.equals(arr[i])){
System.out.println(str+"在数组的第"+(i+1)+"个位置");
break;
}
}
/********** End **********/
}
}
三,交换算法
- 题目:
题目代码:
package step3;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
/********** Begin **********/
//将a的值赋给b b的值赋给a
/********** End **********/
System.out.println(a);
System.out.println(b);
}
}
- 答案:
package step3;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
/********** Begin **********/
//将a的值赋给b b的值赋给a
int sum = a;
a = b;
b = sum;
/********** End **********/
System.out.println(a);
System.out.println(b);
}
}
四,选择排序:
- 题目:
题目代码:
package step4;
import java.util.Arrays;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//动态创建数组
int[] arr = new int[sc.nextInt()];
for(int i = 0 ; i< arr.length ; i++){
arr[i] = sc.nextInt();
}
/********** Begin **********/
/********** End **********/
}
}
- 答案:
package step4;
import java.util.Arrays;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//动态创建数组
int[] arr = new int[sc.nextInt()];
for(int i = 0 ; i< arr.length ; i++){
arr[i] = sc.nextInt();
}
/********** Begin **********/
for(int i = 1 ; i< arr.length ; i++){
for(int j = 0;j<arr.length-i;j++ ){
if(arr[j+1]>arr[j]){
int sum = arr[j];
arr[j] = arr[j+1];
arr[j+1] = sum;
}
}
}
System.out.print(Arrays.toString(arr));
/********** End **********/
}
}
五,冒泡排序
-
题目:
-
答案:
package step5;
import java.util.Arrays;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//动态创建数组
int[] arr = new int[sc.nextInt()];
for(int i = 0 ; i< arr.length ; i++){
arr[i] = sc.nextInt();
}
/********** Begin **********/
for(int i = 1 ; i< arr.length ; i++){
for(int j = 0;j<arr.length-i;j++ ){
if(arr[j]>arr[j+1]){
int sum = arr[j];
arr[j] = arr[j+1];
arr[j+1] = sum;
}
}
}
System.out.print(Arrays.toString(arr));
/********** End **********/
}
}
本文地址:https://blog.csdn.net/weixin_45791445/article/details/110833288