• 2018/12/06 L1-027 出租 Java


    这题使用了很多我之前不会使用的Java类和这些类的用法, 比如说: ArrayList类的contains()方法, 用来判断传入的元素在ArrayList对象中有没有, 有的话就返回true, 没有返回false. 还有静态类Collections, 这个类可以直接调用方法.Collections.sort()方法可以将传入的对象按照升序排列, Collections.reverse()方法可以将传入的对象内的元素翻转, 将两个方法连用可以实现对象的元素按照值的大小进行降序排列.

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Collections;
    
    public class Main {
    
        public static void main(String[] args) throws Exception{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            ArrayList<Integer> alist = new ArrayList<Integer>();
            String str = br.readLine();
            
            for(int i=0; i<str.length(); i++) {
                if(!alist.contains(Integer.valueOf((str.charAt(i)+"")))) {  // contains的作用是如果传入的值在alist中存在, 则返回true
                    alist.add(Integer.valueOf(str.charAt(i)+""));
                }
            }
            Collections.sort(alist);  // 使用sort方法对alist列表进行升序处理
            Collections.reverse(alist);  // 使用reverse方法对alist列表进行翻转处理
            // 通过上面两行代码, 实现了alist降序排列.
            System.out.print("int[] arr = new int[]{");
            for (int i=0; i<alist.size(); i++) {
                if(i==0) {
                    System.out.print(alist.get(i));
                } else {
                    System.out.print("," + alist.get(i));
                }
            }
            System.out.println("};");
            System.out.print("int[] index = new int[]{");
            
            for(int i=0; i<str.length(); i++) {
                if( i==0) {
                    System.out.print(alist.indexOf(Integer.valueOf(str.charAt(i)+"")));  // indexOf得到元素的下标
                } else {
                    System.out.print("," + alist.indexOf(Integer.valueOf(str.charAt(i)+"")));
                }
                
            }
            System.out.print("};");
        }
    
    }
  • 相关阅读:
    对焦过程中消除摩尔纹
    Python3.x:Linux下安装python3.6
    Python3.x:Linux下退出python命令行
    Python3.x:ConfigParser模块的使用
    Python3.x:SQLAlchemy操作数据库
    Python3.x:遍历select下拉框获取value值
    Python3.x:Selenium中的webdriver进行页面元素定位
    Python3.x:selenium获取iframe内嵌页面的源码
    Python3.x:Selenium+PhantomJS爬取带Ajax、Js的网页
    Python3.x:将数据下载到xls时候用xml格式保存一份读取内容
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10078662.html
Copyright © 2020-2023  润新知