@version 1.30 2004-02-27
@author Cay Horstmann
*/import java.util.*;public class EmployeeSortTest
{
public static void main(String[] args)
{
Employee2[] staff = new Employee2[3]; staff[0] = new Employee2("Harry Hacker", 35000);
staff[1] = new Employee2("Carl Cracker", 75000);
staff[2] = new Employee2("Tony Tester", 38000); Arrays.sort(staff); // print out information about all Employee objects
for (Employee2 e : staff)//这里说的是循环语句
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}class Employee2 implements Comparable<Employee2>
{
public Employee2(String n, double s)
{
name = n;
salary = s;
} public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} /**
Compares employees by salary
@param other another Employee object
@return a negative value if this employee has a lower
salary than otherObject, 0 if the salaries are the same,
a positive value otherwise
*/
public int compareTo(Employee2 other)
{
if (salary < other.salary) return -1;
if (salary > other.salary) return 1;
return 0;
} private String name;
private double salary;
}
这是Java2 核心技术第一卷第六章里的demo,有这一句
for (Employee2 e : staff)这里说这是for循环,没见过这样的,原来都是for(;;)这种形式的,这一种具体是什么形式。
知道的帮忙解释一下,谢谢谢谢……
@author Cay Horstmann
*/import java.util.*;public class EmployeeSortTest
{
public static void main(String[] args)
{
Employee2[] staff = new Employee2[3]; staff[0] = new Employee2("Harry Hacker", 35000);
staff[1] = new Employee2("Carl Cracker", 75000);
staff[2] = new Employee2("Tony Tester", 38000); Arrays.sort(staff); // print out information about all Employee objects
for (Employee2 e : staff)//这里说的是循环语句
System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
}
}class Employee2 implements Comparable<Employee2>
{
public Employee2(String n, double s)
{
name = n;
salary = s;
} public String getName()
{
return name;
} public double getSalary()
{
return salary;
} public void raiseSalary(double byPercent)
{
double raise = salary * byPercent / 100;
salary += raise;
} /**
Compares employees by salary
@param other another Employee object
@return a negative value if this employee has a lower
salary than otherObject, 0 if the salaries are the same,
a positive value otherwise
*/
public int compareTo(Employee2 other)
{
if (salary < other.salary) return -1;
if (salary > other.salary) return 1;
return 0;
} private String name;
private double salary;
}
这是Java2 核心技术第一卷第六章里的demo,有这一句
for (Employee2 e : staff)这里说这是for循环,没见过这样的,原来都是for(;;)这种形式的,这一种具体是什么形式。
知道的帮忙解释一下,谢谢谢谢……
http://topic.csdn.net/u/20081129/22/518f37d9-2d34-4380-97bd-a922551bc5eb.html
遍历数组staff中所有的元素,把每个元素赋给e(自动强制类型转换,一样只有父子类才能转换,转换失败抛出异常),分别运行
foreach语法如下:for(type element : array) {System.out.println(element)...}
在J2SE 5.0之前可以使用以下的方式来依次存取数组中的元素:
int a[] = {1, 2, 3, 4}; for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
在J2SE 5.0中可以使用新的foreach语法,这么写:
int a[] = {1, 2, 3, 4}; for (int element : a) {
System.out.println(element);
}
每一次从arr中取出的元素,会自动设定给element,您不用自行判断是否超出了数组的长度,注意element的类型必须与数组元素的元素类型相同。参考的地址http://book.csdn.net/bookfiles/135/1001354622.shtml
我还有一值都弄不明白的问题,那个JDk的版本是从哪里看出来的??我看我的上面是显示的jdk-6u11-windows-i586-p
这是什么意思??什么是JDK5.0
什么1.5,1.4的又是什么意思??
System.out.println(var);
}
循环迭代输出!
CLASSPATH : C:\Program Files\Java\jdk1.6.0_07\lib\dt.jar;C:\Program Files\Java\jdk1.6.0_07\lib\tools.jar;C:\Program Files\Java\jre1.6.0_07\lib\rt.jar;
或者CLASSPATH改成C:\Program Files\Java\jdk1.6.0_07\lib也可以。都试试看 JAVA_HOME : C:\Program Files\Java\jdk1.6.0_07
PATH : C:\Program Files\Java\jdk1.6.0_07\bin 得把上面三个变量设置一下。以上如果是在用户变量里不用重启。如果在系统变量就得重启一次机器。不知道在那里设置请看参考资料。那里有在那里设的。参考资料:
http://zhidao.baidu.com/question/71003939.html
遍历staff中的每一个e,e相当于一个新声明的一个Employee2类型的变量,用来代表staff中的元素
因为这种for不能直接访问下标。。索引。。所以不能对需要的数据进行修改。。
强烈建议使用一般的for。。个人意见