
正文
java代码的对象图 java类与对象代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java语言看下图,对象数组,能根据对象的某种属性对数组对象进行排序吗?
两个方法,一是被比较的类实现Comparable接口,二是使用Comparator比较器。
两种方法各有优劣:
第一种方法:一个类只能有一种比较方法,当需要多种比较方法时,无法实现。
常用的方法是,用第一种方法实现一种默认的,最常用的比较方法。需要其他比较方法时在使用第二种模式。
这是数组类的。
import java.util.Arrays;
import java.util.Comparator;
public class SortDemo {
public static void main(String[] args) {
Student[] array = new Student[] {
new Student("A", 80),
new Student("B", 60),
new Student("C", 70),
new Student("D", 100),
new Student("E", 90) };
printArray(array);
Arrays.sort(array);
System.out.println("After Sort");
printArray(array);
Arrays.sort(array, new ComparatorStudent() {
public int compare(Student s1, Student s2) {
return s2.score - s1.score;
}
});
System.out.println("After Sort2");
printArray(array);
}
private static void printArray(Object[] array) {
for (Object o : array) {
System.out.println(o);
}
}
}
class Student implements ComparableStudent {
public String name;
public int score;
public Student() {
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String toString() {
return "Student " + name + " : " + score;
}
public int compareTo(Student other) {
return this.score - other.score;
}
}
这是容器类的。
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortDemo {
public static void main(String[] args) {
ListStudent list = new ArrayList();
list.add(new Student("A", 80));
list.add(new Student("B", 60));
list.add(new Student("C", 70));
list.add(new Student("D", 100));
list.add(new Student("E", 90));
printList(list);
Collections.sort(list);
System.out.println("After Sort");
printList(list);
Collections.sort(list, new ComparatorStudent() {
public int compare(Student s1, Student s2) {
return s2.score - s1.score;
}
});
System.out.println("After Sort2");
printList(list);
}
private static void printList(ListStudent list) {
for (Student s : list) {
System.out.println(s);
}
}
}
class Student implements ComparableStudent {
public String name;
public int score;
public Student() {
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String toString() {
return "Student " + name + " : " + score;
}
public int compareTo(Student other) {
return this.score - other.score;
}
}
相关问答
Q1: 根据java代码画uml类图,对象的交互图、顺序图,在线等,代码私聊~~!
手里准备一个UML的参考资料和Rose的学习文档,自己一步步来即可。
Q2: 安卓在java代码中怎么添加imageView图片
1、创建imageview对象
2、设置imageview的图片
3、添加到布局中
示例代码
ViewGroup group = (ViewGroup) findViewById(R.id.viewGroup); //获取原来的布局容器
ImageView imageView = new ImageView(this); //创建imageview
imageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); //image的布局方式
imageView.setImageResource(R.drawable.ic_launcher); //设置imageview呈现的图片
group.addView(imageView); //添加到布局容器中,显示图片。
Q3: 怎么用visio画Java代码中类之间的关系图
怎么用visio画Java代码中类之间java代码的对象图的关系图
依赖 --- 两个事物间的语义关系java代码的对象图,对于两个对象X、Yjava代码的对象图,如果对象X发生变化java代码的对象图,可能会引起对另一个对象Y的变化,则称Y依赖于X。
关联 --- 是一种结构关系,指一种对象和另一种对象有联系。给定关联的两个类,可以从其中的一个类的对象访问到另一个类的相关对象。
泛化 --- 一般/特殊关系
Q4: 怎么用java代码模拟一张图片
用java代码模拟一张图片可以这样操作:1.创建BufferedImage类
2.根据BufferedImage类得到一个Graphics2D对象
3.根据Graphics2D对象进行逻辑操作
4.处理绘图
5.将绘制好的图片写入到图片
关于java代码的对象图和java类与对象代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







