public?class?ArrayOperate{
創(chuàng)新互聯(lián)公司2013年成立,先為康保等服務(wù)建站,康保等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為康保企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
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.為數(shù)組元素輸入初始值");
System.out.println("2.求數(shù)組元素的最大值及其下標(biāo)");
System.out.println("3.求數(shù)組元素的最小值及其下標(biāo)");
System.out.println("4.將數(shù)組按大到小排序");
System.out.println("5.將數(shù)組按小到大排序");
System.out.println("0.退出");
System.out.print("請(qǐng)選擇:");
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,對(duì)應(yīng)下標(biāo)是%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,對(duì)應(yīng)下標(biāo)是%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;
/**
* 數(shù)組轉(zhuǎn)置
*
* @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);
}
/**
* 輸入整數(shù)m,n
* 1. 建立一個(gè)擁有m行,n列的矩陣a, 矩陣a中元素的值為隨機(jī)產(chǎn)生的100以內(nèi)的整數(shù);輸出矩陣a
* 2. 并輸出經(jīng)過(guò)轉(zhuǎn)置后的具有n行m列的矩陣b.
* @param m
* @param n
*/
private static void printMatrix(int m, int n) {
System.out.println("-------原數(shù)組--------");
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("-------轉(zhuǎn)置后--------");
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();
}
}
}
【運(yùn)行結(jié)果】:
Please enter m:
3
Please enter n:
4
-------原數(shù)組--------
38 9 6 36
39 31 34 26
67 26 70 82
-------轉(zhuǎn)置后--------
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};
//?定義一個(gè)新數(shù)組
int?[]?b?=?new?int[7];
//?定義一個(gè)變量j,作為數(shù)組b的下標(biāo)
int?j?=?0;
for?(int?i?=?0;?i??a.length;?i++)?{
//?判斷不為0的元素
if?(a[i]?!=?0)?{
//?若不為0,存入新的數(shù)組b
b[j]?=?a[i];
//?b的下標(biāo)加1
j++;
}?
}
//?循環(huán)打印新的數(shù)組b
for?(int?i?=?0;?i??b.length;?i++)?{
System.out.print(b[i]?+?"?");
}
}
}