zoukankan      html  css  js  c++  java
  • 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] + "	");
            }
        }
    
    }
    
    
    
  • 相关阅读:
    LoadRunner测试下载功能点脚本(方法二)
    LoadRunner测试下载功能点脚本(方法一)
    LR翻页脚本并在每页实现业务操作
    LR脚本自定义显示Controller虚拟用户状态
    LR选择哪种方式录制
    LR如何监控tomcat性能
    LR使用Java User协议环境报错Please add the <JDK>in to the path and try again
    HttpServletRequest对象请求转发和HttpServletResponse对象请求重定向之间的区别
    java进制转换
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/wanson/p/13651364.html
Copyright © 2011-2022 走看看