zoukankan      html  css  js  c++  java
  • 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("};");
        }
    
    }
  • 相关阅读:
    Merge sorted ranges
    call_once/once_flag的使用
    对‘boost::serialization::singleton_module::get_lock()’未定义的引用
    C++多线程lock_guard
    长度为0的数组—— Arrays of Length Zero
    Utunbu VLC 播放器播放本机rtp码流
    Utunbu VLC 播放器播放本机h264码流
    Declaration of non-local variable in 'for' loop
    ZFEC--Demo--C语言接口
    malloc-demo
  • 原文地址:https://www.cnblogs.com/huangZ-H/p/10078662.html
Copyright © 2011-2022 走看看