用java编写:以下5道题目
1.有一个整数数组,其中存放着序列1,3,5,7,9,11,13,15,17,19。请将该序列倒序存放并输出。
2.编写一个程序,提示用户输入学生数量姓名和他们的成绩,并按照成绩的降序来打印学生的姓名。
3.编写一个程序,求出整数数组中最小元素的下标。如果这样的元素个数大于1,则返回下标最小的数的下标。
4.现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为:
int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}
5.现在给出两个数组: 数组A:{1,7,9,11,13,15,17,19} ·数组B:{2,4,6,8,10}两个数组合并为数组c,按升序排列。
package lesson1;
public static void test1(){
Integer[] s = {1,3,5,7,9,11,13,15,17,19};
Comparator cmp = new MyComparator();
Arrays.sort(s, cmp);
for(Integer i:s){
System.out.println(i);
}
}
static class MyComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o2 -o1;
}
}
public static void test2(){
try{
Scanner scan = new Scanner(System.in);
System.out.println("请输入学生数:");
int count = scan.nextInt();
int cur = 0;
List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
while(cur<count) {
System.out.println("请输入第" + (cur+1) + "个学生的姓名");
String name = scan.next();
System.out.println("请输入第" + (cur+1) + "个学生的成绩");
float sco = scan.nextFloat();
Map<String,Object> m = new HashMap<String,Object>();
m.put("name", name);
m.put("score", sco);
list.add(m);
cur++;
}
Collections.sort(list, new Comparator<Map<String, Object>>() { //排序
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
if (o1.get("score") == null && o2.get("score") == null)
return 0;
if (o1.get("score") == null)
return -1;
if (o2.get("score") == null)
return 1;
return Float.valueOf(o2.get("score").toString()).compareTo(Float.valueOf(o1.get("score").toString()));
}
});
for(Map<String, Object> m :list){
System.out.println("学生姓名:"+m.get("name")+",成绩:"+m.get("score"));
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void test3(){
Integer[] a1 = {1,3,5,7,9,1,1,15,17,19};
Integer[] a2 = new Integer[a1.length];
for(int i=0;i<a1.length;i++){
a2[i] = a1[i];
}
Arrays.sort(a1);
Integer min = a1[0];
for(int i=0;i<a2.length;i++){
if(min==a2[i]){
System.out.println("最小元素下标:"+i);
}
}
}
public static void test4(){
Integer[] a1 = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
List<Integer> list = new ArrayList<Integer>();
for(int i=0;i<a1.length;i++){
if(0!=a1[i]){
list.add(a1[i]);
}
}
Integer[] a2 = new Integer[list.size()];//新数组
for(int i=0;i<list.size();i++){
a2[i] = list.get(i);
}
for(Integer s :a2){
System.out.print(s+" ");
}
}
public static void test5(){
Integer[] a1 = {1,7,9,11,13,15,17,19};
Integer[] a2 = {2,4,6,8,10};
List<Integer> list = new ArrayList<Integer>();//所谓合并,区分是否去重复,这里没做去重复处理
for(Integer i:a1){
list.add(i);//
// if(!list.contains(i)){//去重复处理,需要再加
//
// }
}
for(Integer i:a2){
list.add(i);//
}
Integer[] a3 = new Integer[list.size()];//新数组
for(int i=0;i<list.size();i++){
a3[i] = list.get(i);
}
Arrays.sort(a3);
}