JAVA中Arrays类:使用之前需要在程序前面加上
import java.util.Arrays;
一、Arrays类中包含的方法:
1、数组排序语法:
方法:Arrays.sort(<数组名>);
sort()是Arrays类的一个排序方法,默认为升序。
2、数组搜索语法:
方法:Arrays.binarySearch(<数组名>,<关键字>);
Arrays.binarySearch()是Arrays类的搜索方法,查找目标数据在数组中的位置。
3、数组的打印:Arrays.toString(数组名):
二、foreach语句
1、语句格式:
foreach的语句格式:
for(元素类型t 元素变量x : 遍历对象obj){
引用了x的java语句;
}
并不是所有的数组都可以使用foreach语句。
三、方法
1、此处的方法也就类似于C/C++中的类中的成员函数,只是名字不同,基本规则相同,若学过C语言或者C++便很容易理解。
2、无参无返回值方法,有参无返回值方法,无参有返回值方法,有参有返回值方法,这四种方法的用法,以及方法中的重载。
无参无返回值用法:
public class HelloWorld {
public static void main(String[] args) {
// 创建对象,对象名为hello
HelloWorld hello = new HelloWorld();
hello.showMyLove();
// 调用方法
}
/*
* 定义无参无返回值的方法
*/
public void showMyLove() {
System.out.println("我爱你!");
}
}
有参无返回值方法:
public class HelloWorld {
public static void main(String[] args) {
// 创建对象,对象名为hello
HelloWorld hello = new HelloWorld();
// 调用方法,传入两门课程的成绩
hello.calcAvg(94, 81);
}
/*
* 功能:计算两门课程考试成绩的平均分并输出平均分
* 定义一个包含两个参数的方法,用来传入两门课程的成绩
*/
public void calcAvg(int a,int b){
double c = (a+b)/2.0;
System.out.println("平均分:"+ c);
}
有参有返回值方法:
import java.util.Arrays;
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
int[] scores={79,52,98,81};
//调用方法,传入成绩数组,并获取成绩的个数
int count= hello.sort(scores);
System.out.println("共有"+count+"个成绩信息!");
}
/*
* 功能:将考试成绩排序并输出,返回成绩的个数
* 定义一个包含整型数组参数的方法,传入成绩数组
* 使用Arrays类对成绩数组进行排序并输出
* 方法执行后返回数组中元素的个数
*/
public int sort( int[] scores){
Arrays.sort(scores);
System.out.println(Arrays.toString(scores));
//返回数组中元素的个数
return scores.length;
}
}
方法的重载:
public class HelloWorld {
public static void main(String[] args) {
String name="mooc";
int age=18;
// 创建对象
HelloWorld hello = new HelloWorld();
// 调用无参的方法
hello.print();
// 调用带有一个字符串参数的方法
hello.print(name);
// 调用带有一个整型参数的方法
hello.print(age);
}
public void print() {
System.out.println("无参的print方法");
}
public void print(String name) {
System.out.println("带有一个字符串参数的print方法,参数值为:" + name);
}
public void print(int age) {
System.out.println("带有一个整型参数的print方法,参数值为:" + age);
}
}
四、类和对象
1、可以看成是创建JAVA对象的模板。其定义与C++中类的定义类似,对照着学会简单一点,但不要把两者看成完全一样。
2、类中的构造方法,相当于构造函数
五、JAVA中static使用
1、静态方法:
public class HelloWorld {
public static void print(){
//使用static关键字声明静态方法
System.out.println("欢迎您访问我的博客!");
}
public static void main(String[] args){
//直接使用类名调用静态方法
HelloWorld.print();
//也可以通过对象名调用,推荐使用类名调用的方式
HelloWorld demo = new HelloWorld();
demo.print();
}
}
2、静态方法可以直接调用同类中的静态变量,但不能直接调用同类中的非静态变量
public class HelloWorld {
String name = "爱博客";//非静态变量name
static String hobby = "web";//静态变量hobby
public static void print(){
//使用static关键字声明静态方法
System.out.println("欢迎您访问我的博客!"+name);
//上面那句话错误,不能直接调用静态变量
System.out.println("欢迎您访问我的博客!"+hobby);
//可以直接调用静态变量
}
public static void main(String[] args){
}
}
3、如果希望在静态方法中调用非静态变量,可以通过创建类的对象,然后通过对象来访问非静态变量。
public class HelloWorld {
String name;
public static void print(){
//使用static关键字声明静态方法
HelloWorld hello = new HelloWorld();
System.out.println("欢迎您访问我的博客!"+hello.name);
//可以调用静态变量
}
public static void main(String[] args){
}
}
4、 在普通成员方法中,则可以直接访问同类的非静态变量和静态变量
public class HelloWorld {
String name;
static String hobby = "爱博客";
public static void print(){
//使用static关键字声明静态方法
HelloWorld hello = new HelloWorld();
System.out.println("欢迎您访问我的博客!"+hello.name);
//可以调用静态变量
}
public void show(){
//普通成员方法可以直接访问非静态变量和静态变量
System.out.println(name);
System.out.println(hobby);
}
public static void main(String[] args){
}
}
5、静态方法中不能直接调用非静态方法,需要通过对象来进行访问静态方法
public class HelloWorld {
String name;
static String hobby = "爱博客";
public static void print(){
//使用static关键字声明静态方法
HelloWorld hello = new HelloWorld();
System.out.println("欢迎您访问我的博客!"+hello.name);
//可以调用静态变量
}
public void show(){
//普通成员方法可以直接访问非静态变量和静态变量
System.out.println(name);
System.out.println(hobby);
}
public static void main(String[] args){
//普通成员方法必须通过对象来调用
HelloWorld hello = new HelloWorld();
hello.show();
//可以直接调用静态方法
print();
}
}