A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

李票 发表于 2015-3-21 20:39
加油复习java基础  快报名了

加油。
回复 使用道具 举报
黑龙斩铁 发表于 2015-3-21 21:24
上传答案,自学真累,没做多少注释,请老师体谅啊

写的不错,加油。
回复 使用道具 举报
随心i 发表于 2015-3-22 16:29
对ArrayList还不是太懂,请阳哥多多指教

挺好,继续努力。集合是javase最重要的基础之一。
回复 使用道具 举报
lmcci 发表于 2015-3-23 21:51
领题,不知道会不会做呢。

加油。
回复 使用道具 举报
回复 使用道具 举报

回复以后,在看帖子内容应该就可以看到题目了。
回复 使用道具 举报
黑马小符 发表于 2015-3-23 23:41
有的话要怎么报名啊?

关于如何提交答案,在题目中有说明。
回复 使用道具 举报
小川行 发表于 2015-3-23 23:48
题做完了,上传完毕,请老师查阅!

写的不错:
  1. package com.test;

  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.Iterator;
  6. import java.util.List;

  7. public class MySort {

  8.         public static void main(String[] args) {
  9.                 List<User> list=new ArrayList<User>();
  10.                 list.add(new User("wangwu",24));
  11.                 list.add(new User("wangwu",23));
  12.                 list.add(new User("wangwu",27));
  13.                 list.add(new User("wangwu",29));
  14.                 list.add(new User("wangwu",21));
  15.                 Print(list);
  16.                 Collections.sort(list, new MyCompare());//调用排序方法,传入自己的比较器
  17.                 Print(list);
  18.         }
  19.         //定义一个打印方法
  20.         public static void Print(List<User> list){
  21.                 Iterator<User> it=list.iterator();
  22.                 System.out.print("[");
  23.                 while(it.hasNext()){
  24.                         User u=it.next();
  25.                         System.out.print(u.getName()+":"+u.getAge()+" ");
  26.                 }
  27.                 System.out.println("]");
  28.         }
  29. }
  30. class User{
  31.         private String name;
  32.         private int age;
  33.         User(String name,int age){
  34.                 this.age=age;
  35.                 this.name=name;
  36.         }
  37.         public String getName(){
  38.                 return this.name;
  39.         }
  40.         public int getAge(){
  41.                 return this.age;
  42.         }
  43. }
  44. //定义自己的比较器
  45. class MyCompare implements Comparator<User>{
  46.         public int compare(User u1,User u2){
  47.                 int num=u1.getAge()-u2.getAge();
  48.                 if(num==0)
  49.                         return u1.getName().compareTo(u2.getName());
  50.                 return num;
  51.         }
  52. }
复制代码
回复 使用道具 举报
pkdo0 发表于 2015-3-24 01:05
代码写好了,请阳哥鉴定,第一次提交题目,如果有格式问题请阳哥多包涵另外给点技术分吧,第一次啊,阳哥
...

正确:
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;
  4. import java.util.Iterator;



  5. public class Sort {

  6.         public static class User
  7.         {
  8.                  String name;
  9.                  int age;
  10.                  
  11.                  public User(String name,int age)
  12.                  {
  13.                          this.name=name;
  14.                          this.age=age;
  15.                  }
  16.                  public int getAge()
  17.                  {
  18.                          return age;
  19.                  }
  20.                  public void setAge(int age)
  21.                  {
  22.                          this.age = age;
  23.                  }
  24.                  public String getName()
  25.                  {
  26.                          return name;
  27.                  }
  28.                  public void setName(String name)
  29.                  {
  30.                          this.name = name;
  31.                  }
  32.                  
  33.                 //重写toString实现打印User类对象
  34.                  public String toString()
  35.                  {  
  36.                         return "("+name+", "+age+")";  
  37.                  }  
  38.         }
  39.        
  40.         //实现user之间根据年龄比较大小的类
  41.         public static class UserComparator implements Comparator
  42.         {
  43.                  public int compare(Object arg0, Object arg1)
  44.                  {
  45.                          User user0=(User)arg0;
  46.                          User user1=(User)arg1;
  47.                          return user1.getAge()-user0.getAge();
  48.                  }
  49.         }
  50.        
  51.        
  52.         public static void main(String[] arg)
  53.         {
  54.                 ArrayList<User> userLists = new ArrayList<User>();
  55.                 User u1 = new User("a",15);
  56.                 User u2 = new User("b",19);
  57.                 User u3 = new User("c",12);
  58.                 User u4 = new User("d",7);
  59.                 userLists.add(u1);
  60.                 userLists.add(u2);
  61.                 userLists.add(u3);
  62.                 userLists.add(u4);
  63.                
  64.                 UserComparator comparator = new UserComparator();
  65.                 Collections.sort(userLists, comparator);
  66.                
  67.                 Iterator<User> it = userLists.iterator();  
  68.         while(it.hasNext())  
  69.         {  
  70.             System.out.println(it.next());  
  71.         }  
  72.         }
  73. }
复制代码
回复 使用道具 举报
liyanghml 发表于 2015-3-24 07:02
看看 我现在到什么程度了

正确:

  1. //已知User类,该类有name(String类型)和age(int类型)两个属性,
  2. //请编写代码实现给ArrayList<User>排序,要求按照User对象的age的倒序排序。

  3. import java.util.*;

  4. public class UserDemo
  5. {
  6.         public static void main(String []args)
  7.         {
  8.                 List<User> UserList = new ArrayList<User>();
  9.                   UserList.add(new User("张三", 17));
  10.                   UserList.add(new User("李四", 20));
  11.                   UserList.add(new User("王五", 23));
  12.                   UserList.add(new User("赵六", 17));

  13.                   Collections.sort(UserList,new ageSort());
  14.                   for(User u : UserList)
  15.                   {
  16.                          System.out.println(u.getName()+"::"+u.getAge());
  17.                   }
  18.         }
  19. }
  20. class ageSort implements Comparator<User>
  21. {
  22.         public int compare(User u1,User u2)
  23.         {
  24.                 if(u1.getAge()==u2.getAge())
  25.                         return u1.getName().compareTo(u2.getName());
  26.                 if(u1.getAge()>u2.getAge())
  27.                         return -1;
  28.                 return 1;
  29.         }
  30. }

  31. //创建user类
  32. class User
  33. {
  34.         private String name;
  35.         private int age;
  36.         User(String name,int age)
  37.         {
  38.                 this.name = name;
  39.                 this.age = age;
  40.         }
  41.         public String getName()
  42.         {
  43.                 return this.name;
  44.         }
  45.         public int getAge()
  46.         {
  47.                 return this.age;
  48.         }
  49. }
复制代码
回复 使用道具 举报

正确:
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.Comparator;


  4. class User{
  5.         String name;
  6.         int age;
  7.         public User(String name, int age)
  8.         {
  9.                 this.name = name;
  10.                 this.age = age;
  11.         }
  12.         static void display(ArrayList<User> list)
  13.         {
  14.                 for(User user:list)
  15.                 {
  16.                         System.out.println(user.age + ":" + user.name);
  17.                 }
  18.         }
  19. }

  20. class MyComparator implements Comparator<User>
  21. {
  22.         public int compare(User stu1, User stu2)
  23.         {
  24.                 if(stu1.age < stu2.age)
  25.                 {
  26.                         return 1;
  27.                 }
  28.                
  29.                 if(stu1.age == stu2.age)
  30.                 {
  31.                         return 0;
  32.                 }
  33.                
  34.                 if(stu1.age > stu2.age)
  35.                 {
  36.                         return -1;
  37.                 }
  38.                 return 0;
  39.         }
  40. }

  41. public class Test_03_23 {

  42.         public static void main(String[] args) {
  43.                 MyComparator comparator = new MyComparator();
  44.                 ArrayList<User> myList = new ArrayList<User>();
  45.                 myList.add(new User("Tom", 18));
  46.                 myList.add(new User("May", 50));
  47.                 myList.add(new User("Mike", 40));
  48.                 myList.add(new User("Jack", 17));
  49.                 Collections.sort(myList, comparator);
  50.                 User.display(myList);
  51.                
  52.         }

  53. }
复制代码
回复 使用道具 举报
脚踏实地2015 发表于 2015-3-25 22:49
黑马,让我们一起起航,改变自己,加油

加油。
回复 使用道具 举报
qkdavid 发表于 2015-4-1 20:33
阳哥.我又来混口饭了..要存.25技术分...

加油。
回复 使用道具 举报
wuliang 发表于 2015-4-1 22:11
今天刚学了Arraylist ,实现起来有点浪费资源,望杨哥给过。。。。。以后学习了更好的方法再优化下
...

加油。
回复 使用道具 举报
李明全 发表于 2015-4-3 00:05
已读,阳哥,518上海黑马见。

加油,java基础能学多好学多好。基础是最重要的。
回复 使用道具 举报
GXJ1236987450 发表于 2015-4-12 16:00
刚学java的小水鸭来领题看看

加油。
回复 使用道具 举报
陈建民 发表于 2015-4-20 21:16
哈喽,阳哥大神你好

你好。
回复 使用道具 举报
陈建民 发表于 2015-4-20 21:16
哈喽,阳哥大神你好

你好。
回复 使用道具 举报
脱离梦幻泡影 发表于 2015-8-25 11:11
王震阳老师很崇拜你啊

是吗,努力就行
回复 使用道具 举报
sxwnzbn 发表于 2015-9-16 10:13
阳哥,现在回答往期所有的题然后发给你,还能领技术分吗?

最近的十期可以的。
回复 使用道具 举报
123
您需要登录后才可以回帖 登录 | 加入黑马