• java之数组常见的操作


    package com.china.passion;
    
    import java.util.Scanner;
    
    public class HelloArray {
        public static void main(String[] args) {
            double[] myElements = new double[5];
            Scanner input = new Scanner(System.in);
    //        人工赋值
    /*        System.out.println("Enter	"+myElements.length+"	values:	");
            for (int i = 0; i < myElements.length; i++) {
                myElements[i]=input.nextDouble();
            }*/
    //        用随机数生成初始值
            for (int i = 0; i < myElements.length; i++) {
                myElements[i] = Math.random() * 101;
            }
    //        输出数组元素
            for (int i = 0; i < myElements.length; i++) {
                System.out.print(myElements[i] + "	");
            }
            System.out.println();
            for(double nums:myElements){
                System.out.print(nums+"	");
            }
    //        换行
            System.out.println();
    
    //       数组元素求和
            double total = 0;
            for (int i = 0; i < myElements.length; i++) {
                total += myElements[i];
            }
            System.out.println("数组元素和为:	" + total);
    
    //    找数组元素最大值
            double maxValue = myElements[0];
            int indexOfMax = 0;
            for (int i = 0; i < myElements.length; i++) {
                if (myElements[i] > maxValue) {
                    maxValue = myElements[i];
                    indexOfMax = i;
                }
    
            }
            System.out.println("最大值为:	" + maxValue);
            System.out.println("最大值下标为:	" + indexOfMax);
    //        找数组元素最大值
            double minValue = myElements[0];
            int indexOfMin = 0;
            for (int i = 0; i < myElements.length; i++) {
                if (myElements[i] < minValue) {
                    minValue = myElements[i];
                    indexOfMin = i;
                }
    
            }
            System.out.println("最小值为:	" + minValue);
            System.out.println("最小值下标为:	" + indexOfMin);
    //        随机洗牌
            for (int i = 0; i < myElements.length - 1; i++) {
    //            随机生成下标
                int j = (int) (Math.random() * myElements.length);
    //            用myElements[j]和myElements[i]交换
                double temp = myElements[i];
                myElements[i] = myElements[j];
                myElements[j] = temp;
            }
            System.out.println("洗牌后的数组输出元素为:	");
            for (int i = 0; i < myElements.length; i++) {
                System.out.print(myElements[i] + "	");
            }
    //        数组元素左移或者右移
    //        固定一个元素
            double tmp=myElements[0];
            for (int i = 1; i < myElements.length; i++) {
                myElements[i-1]=myElements[i];
            }
            myElements[myElements.length-1]=tmp;
    //        左移之后的数组元素如下
            System.out.println();
            System.out.println("左移之后的数组元素如下:");
            for (int i = 0; i < myElements.length; i++) {
                System.out.print(myElements[i] + "	");
            }
        }
    
    }
    
    
    
  • 相关阅读:
    Rocket
    Rocket
    Rocket
    Rocket
    Scala
    Rocket
    Rocket
    Rocket
    Rocket
    Rocket
  • 原文地址:https://www.cnblogs.com/wanson/p/13651364.html
Copyright © 2020-2023  润新知