遍历数组中每个元素,以下两种表达方式是一样的
for (int i = 0; i <= myArray.length; i++) {
// Do something
}
for (String s : myArray) {
// Do something
}
return的使用,在主函数中赋值带return的函数
// Method will return an int value
public static int findProduct(int num1, int num2) {
return num1 * num2;
}
public static void main(String[] args) {
int product = findProduct(3,4);
System.out.println(product); // Prints: 12
}
String的方法
- .length() 长度
- .concat() 连接
- .equals() 等于 返回true/false
- .indexOf() 位置
- .charAt() 返回String某个位置的值
- .substring() 返回的String的一部分
- .toUpperCase() / .toLowerCase() 大小写 ```java //.concat() String name = “Code”;
//一定要重新赋一下值 name = name.concat(“cademy”); System.out.println(name); // Prints: Codecademy
//indexOf() letters.indexOf(“EFG”)
//.charAt()
String currency = "Yen";
System.out.println(currency.charAt(2)); // Prints: n
//.substring() String line = “It was the best of times, it was the worst of times.”; line.substring(26) line.substring(7, 24)
//大小写变化
String input = "Cricket!";
input.toUpperCase() input.toLowerCase()
数组
```java
//定义
int[] lottoNumbers;
String[] clothingItems;
//声明
int[] lottoNumbers = {12, 29, 4, 38, 3};
String[] clothingItems = {"Huipil", "Beanie", "Kimono", "Sari"};
//创建一个空数组
String[] menuItems = new String[5];
//改变其中一个元素
clothingItems[1] = "Sweater";
//长度
lottoNumbers.length
//遍历数组
int[] lottoNumbers = {12, 29, 4, 38, 3};
for (int num: lottoNumbers) {
System.out.println(num);
}
二维数组
//int[][] nums;
//int[][] nums = {{10, 9, 8}, {7, 6, 5}, {4, 3, 2}};
nums[0][1]
//更新数组
char[][] letters = {{'A', 'a'}, {'B', 'x'}, {'C', 'c'}};
letters[1][1] = 'b';
//创建空数组,加入元素
int[][] intArray = new int[2][3];
intArray[0][0] = 1;
intArray[0][1] = 2;
intArray[0][2] = 4;
intArray[1][0] = 1;
intArray[1][1] = 3;
intArray[1][2] = 6;
//遍历AaBbCc
char[][] letters = {{'A', 'a'}, {'B', 'b'}, {'C', 'c'}};
for (int i = 0; i < letters.length; i++){
for (int j = 0; j < letters[0].length; j++){
System.out.print(letters[i][j]);
}
}
//遍历AaBbCc
char[][] letters = {{'A', 'a'}, {'B', 'b'}, {'C', 'c'}};
for (int i = 0; i < letters[0].length; i++){
for (int j = 0; j < letters.length; j++){
System.out.print(letters[j][i]);
}
}
四类访问修饰符
构造函数
public class Car {
// Constructor method:
public Car() {
System.out.println("I'm a constructor!");
}
public static void main(String[] args) {
//唤醒构造函数并打印,Prints: I'm a constructor!
Car ferrari = new Car();
}
}
创建实例字段
class Cat {
// instance fields:
String noise = "meow";
int numLives = 9;
public Cat(){
// Instructions for creating an instance of Cat goes here:
}
public static void main(String[] args) {
Cat myCat = new Cat(); // Access instance variable of an object:
System.out.println(myCat.noise); // Prints: meow
}
}
创建实例字段(传递参数)
class Cat {
// Instance fields:
String noise;
int numLives = 9;
// Constructor takes in one String parameter
public Cat(String animalNoise){
// Assign instance variable to parameter value:
noise = animalNoise;
}
public static void main(String[] args) {
// Send argument to constructor when creating an object:
Cat firstCat = new Cat("mew");
// Send argument to constructor when creating another object:
Cat secondCat = new Cat( "mow");
System.out.println(firstCat.noise); // Prints: mew
System.out.println(secondCat.noise); // Prints: mow
}
}
this的使用,在一个类里面传递参数
class Cat {
String noise;
int numLives = 9;
// Parameter has same name as the instance variable
public Cat(String noise){
// Assign instance variable to parameter value:
this.noise = noise;
}
}
给对象赋予行为,就要使用动态(非静态)方法
class Cat {
String noise;
int numLives = 9;
// Non-static method:
public void speak() {
System.out.println(noise);
}
public Cat(String animalNoise){
noise = animalNoise;
}
public static void main(String[] args) {
Cat myCat = new Cat("mew");
//在对象上调用非静态方法
myCat.speak(); // Prints: mew
}
}
class Whale {
String whaleSpecies;
int whaleWeight;
public Whale(String name, int weight) {
whaleSpecies = name;
whaleWeight = weight;
}
public String toString(){
return "This is a " + whaleSpecies + " whale which weighs about " + whaleWeight + " pounds.";
}
public static void main(String[] args){
Whale whale1 = new Whale("narwhal", 2100);
Whale whale2 = new Whale("beluga", 3000);
System.out.println(whale1);
System.out.println(whale2);
}
}
/*Output:
This is a narwhal whale which weighs about 2100 pounds.
This is a beluga whale which weighs about 3000 pounds.
*/
使用private变量需要使用get和set
public class Dog {
private String name;
// Other methods and constructors
public String getName() {
return name;
}
public static void main(String[] args) {
Dog myDog = new Dog("Lassie");
System.out.println(myDog.getName()); // Prints: Lassie
}
}
public class Dog {
private String name;
// Other methods and constructors
public void setName(String newName) {
name = newName;
}
public static void main(String[] args){
Dog myDog = new Dog("Cujo");
myDog.setName("Lassie");
}
}
ublic class Dog {
private String name;
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String newName) {
name = newName;
}
public static void main(String[] args) {
Dog myDog = new Dog("Lassie");
myDog.setName("Cujo");
System.out.println(myDog.getName());
}
}
范围
public class MinHeap {
public ArrayList<Integer> heap;
public int size;
public MinHeap() {
// Body of constructor
}
public void add(int value) {
heap.add(value);
size++;
bubbleUp();
}
//private的这个函数不能在外使用
private void bubbleUp() {
// Body of .bubbleUp() method
}
}
public class TrackAges {
public static void main(String[]args) {
MinHeap ages = new MinHeap();
// Can call public MinHeap .add() method here:
ages.add(42);
ages.add(15);
ages.add(27);
// Can’t call private MinHeap .bubbleUp() method here
// Don’t need to because .add() calls .bubbleUp()!
}
}
继承的两种情况
class Shape {
int numSides;
Shape(int numSides) {
this.numSides = numSides;
}
}
class Triangle extends Shape {
Triangle() {
// Use super() to call the Shape constructor:
super(3);
}
public static void main(String[] args) {
Shape sq = new Shape(4);
Triangle tri = new Triangle();
System.out.println(sq.numSides); // Prints: 4
System.out.println(tri.numSides); // Prints: 3
}
}
class Shape {
Shape() {
System.out.println("I am a shape!");
}
}
// Make Triangle a subclass of Shape:
class Triangle extends Shape {
Triangle() {
System.out.print("I am a triangle!");
}
public static void main(String[] argos) {
Shape sq = new Shape(); // Prints: I am a shape!
Triangle tri = new Triangle();
/* Prints:
I am a shape!
I am a triangle!
*/
}
}