大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class ArrayOperate{
创新互联公司2013年成立,先为康保等服务建站,康保等地企业,进行企业商务咨询服务。为康保企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
static final int COUNT=20;
static int arr[]=new int[COUNT];
static java.util.Scanner sc = new java.util.Scanner(System.in);
public static void main(String args[]){
while(true){
System.out.println();
System.out.println("1.为数组元素输入初始值");
System.out.println("2.求数组元素的最大值及其下标");
System.out.println("3.求数组元素的最小值及其下标");
System.out.println("4.将数组按大到小排序");
System.out.println("5.将数组按小到大排序");
System.out.println("0.退出");
System.out.print("请选择:");
int sel = sc.nextInt();
switch(sel){
case 1:
doInput();
break;
case 2:
doGetMax();
break;
case 3:
doGetMin();
break;
case 4:
doOrderMax();
break;
case 5:
doOrderMin();
break;
case 0:
return;
default:
;
}
}
}
static boolean isInputed(){
int sum=0;
for(int i=0; iCOUNT; i++){
sum+=Math.abs(arr[i]);
}
return (sum!=0);
}
static void doGetMax(){
int max=Integer.MIN_VALUE, idx=-1;
for(int i=0; iCOUNT; i++){
if(arr[i]max){
max=arr[i];
idx=i;
}
}
System.out.printf("最大值是%d,对应下标是%d\r\n", max, idx);
}
static void doGetMin(){
int max=Integer.MAX_VALUE, idx=-1;
for(int i=0; iCOUNT; i++){
if(arr[i]max){
max=arr[i];
idx=i;
}
}
System.out.printf("最小值是%d,对应下标是%d\r\n", max, idx);
}
static void doOrderMax(){
int ar2[]=java.util.Arrays.copyOf(arr, COUNT);
for(int i=0; iCOUNT; i++){
for(int j=i+1; jCOUNT; j++){
if(ar2[j]ar2[i]){
int tmp=ar2[i];
ar2[i]=ar2[j];
ar2[j]=tmp;
}
}
}
for(int i=0; iCOUNT; i++){
System.out.printf("%d ", ar2[i]);
}
System.out.println();
}
static void doOrderMin(){
int ar2[]=java.util.Arrays.copyOf(arr, COUNT);
for(int i=0; iCOUNT; i++){
for(int j=i+1; jCOUNT; j++){
if(ar2[j]ar2[i]){
int tmp=ar2[i];
ar2[i]=ar2[j];
ar2[j]=tmp;
}
}
}
for(int i=0; iCOUNT; i++){
System.out.printf("%d ", ar2[i]);
}
System.out.println();
}
}
package com.linc.baidu;
import java.util.Random;
import java.util.Scanner;
/**
* 数组转置
*
* @author Lin.C
* @date 2020/6/7 13:48
*/
public class Answer001 {
/**
* Main
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter m:");
int m = Integer.parseInt(sc.next());
System.out.println("Please enter n:");
int n = Integer.parseInt(sc.next());
printMatrix(m, n);
}
/**
* 输入整数m,n
* 1. 建立一个拥有m行,n列的矩阵a, 矩阵a中元素的值为随机产生的100以内的整数;输出矩阵a
* 2. 并输出经过转置后的具有n行m列的矩阵b.
* @param m
* @param n
*/
private static void printMatrix(int m, int n) {
System.out.println("-------原数组--------");
Random random = new Random();
int a[][] = new int[m][n];
for (int i = 0; i m; i++) {
for (int j = 0; j n; j++) {
a[i][j] = random.nextInt(100);
System.out.print(a[i][j] + " ");
}
System.out.println();
}
int b[][] = new int[n][m];
System.out.println("-------转置后--------");
for (int j = 0; j n; j++) {
for (int i = 0; i m; i++) {
b[j][i] = a[i][j];
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
【运行结果】:
Please enter m:
3
Please enter n:
4
-------原数组--------
38 9 6 36
39 31 34 26
67 26 70 82
-------转置后--------
38 39 67
9 31 26
6 34 70
36 26 82
class NewArray {
public static void main (String[] args) {
int [] a = {1, 3, 4, 5, 0, 0, 6, 6, 0, 5, 4, 7, 6, 7, 0, 5};
// 定义一个新数组
int [] b = new int[7];
// 定义一个变量j,作为数组b的下标
int j = 0;
for (int i = 0; i a.length; i++) {
// 判断不为0的元素
if (a[i] != 0) {
// 若不为0,存入新的数组b
b[j] = a[i];
// b的下标加1
j++;
}
}
// 循环打印新的数组b
for (int i = 0; i b.length; i++) {
System.out.print(b[i] + " ");
}
}
}