日期:2014-05-20  浏览次数:20638 次

新手 几道编程题
1、写一个通用方法把任何一个类似“abcdefg”的字符串倒叙排。

2、把字符数组String[] str={“6”,”5”,”3”,”1”,”2”,4”}中的字符数字按照从小到大调整位置。两种方法

3 把Lis t 容器中的数字元素“6” ”5” ”3” ”1” ”2” ”4”,按照从大到小排序进行重新存放。

4、有一个接口,接口中有一个抽象的方法,然后一个普通的类implements该接口,但该类并不自己去实现从接口中继承的抽象方法,如何来完成,举个例子。

有一个接口和接口的子类,接口中有一个print的方法,子类实现该方法, 而且子类中有一个自己声明的方法make,编程运用上行机制调用子类中的方法print,同时解释在上行时能否调用make,为什么?

5、对一个Ma p 类型的容器中元素“6” ”5” ”3” ”1” ”2” ”4”进行排序。




------解决方案--------------------
Java code

//写一个通用方法把任何一个类似“abcdefg”的字符串倒叙排。
public class StringDesc {
    public static void main(String[] args) {
        String s = "abcdefg";
        System.out.println(desc(s));
    }
    
    public static String desc(String s){
        String str = "";
        int i;
        for(i=s.length()-1;i>=0;i--){
            str = str + String.valueOf(s.charAt(i));
        }
        return str;
    }
}

------解决方案--------------------
3 把Lis t 容器中的数字元素“6” ”5” ”3” ”1” ”2” ”4”,按照从大到小排序进行重新存放。

Colletions类中算法排序

4、有一个接口,接口中有一个抽象的方法,然后一个普通的类implements该接口,但该类并不自己去实现从接口中继承的抽象方法,如何来完成,举个例子。

该类为抽象类,交给下面实现方法。

4.1有一个接口和接口的子类,接口中有一个print的方法,子类实现该方法, 而且子类中有一个自己声明的方法make,编程运用上行机制调用子类中的方法print,同时解释在上行时能否调用make,为什么?

调用print用父类引用指向子类对象就是了,此时不能调用make因为接口中没有定义
除非子类引用指向子类对象才可使用make();
Java code

public class Test implements FTest{

    public void fPrint() {
        System.out.println("Hi,CSDN!");
    }
    
    public void make(){
        System.out.println("Bye,CSDN!");
    }
    
    public static void main(String args[]){
        FTest ft1 = new Test();
        ft1.fPrint();
//        ft1.make();
        Test t1 = new Test();
        t1.make();
    }
}
interface FTest{
    void fPrint();
}

------解决方案--------------------
1、写一个通用方法把任何一个类似“abcdefg”的字符串倒叙排。
 String str = "是对 方身份";
 List<String> list = Arrays.asList(str.split(""));
 Collections.reverse(list);
 String s = list.toString();
 s = s.replaceAll("[\\[\\],]","");
 System.out.println(s);

2、把字符数组String[] str={“6”,”5”,”3”,”1”,”2”,4”}中的字符数字按照从小到大调整位置。两种方法
 
String[] str = {"6","5","3","1","2","4"};
Arrays.sort(str);
System.out.println(Arrays.toString(str));

List<String> list = Arrays.asList(str);
Collections.sort(list);
System.out.println(list.toString());

3 把Lis t 容器中的数字元素“6” ”5” ”3” ”1” ”2” ”4”,按照从大到小排序进行重新存放。

String[] str = {"6","5","3","1","2","4"};
List<String> list = Arrays.asList(str);
Collections.sort(list);
Collections.reverse(list);
System.out.println(list.toString());