求多个数组的交集
class Solution {
int[] temp = new int[1001];
public List<Integer> intersection(int[][] nums) {//List< Integer> List是一个接口 <>中表示了List里面放的对象是什么类型。Integer:Integer是java为int提供的封装类
int n = nums.length;
List<Integer> ans = new ArrayList();
//ArrayList类是一个动态数组,t是List接口的一个实现类。
for(int i = 0; i<n; ++i) {
for(int j = 0; j<nums[i].length; ++j) {
++temp[nums[i][j]];
}
}
for(int i = 1; i<1001; ++i) {
if(temp[i]==n) {
ans.add(i);//调用 List接口里面的内置函数(add,get等方法)
}
}
return ans;
}
}
class Solution {
public String[] findWords(String[] words) {
List<String> list = new ArrayList<String>();
String rowIdx = "12210111011122000010020202";
for (String word : words) {
boolean isValid = true;
char idx = rowIdx.charAt(Character.toLowerCase(word.charAt(0)) - 'a');
//charAt() 方法用于返回指定索引处的字符
//Character 类用于对单个字符进行操作 character.tolowerCase()方法转小写
for (int i = 1; i < word.length(); ++i) {
if (rowIdx.charAt(Character.toLowerCase(word.charAt(i)) - 'a') != idx) {
isValid = false;
break;
}
}
if (isValid) {
list.add(word);//.add():将元素追加到列表的末尾
}
}
String[] ans = new String[list.size()];
for (int i = 0; i < list.size(); ++i) {
ans[i] = list.get(i);//获取List中的第i+1个元素
}
return ans;
}
}
Java继承和多态之子类继承性
Dod.java
public class Dog extends Animal{
//定义小狗的性别sex,字符串类型
private String sex;
// 定义获取和设置小狗性别的方法。
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
// 如果子类没有覆盖父类的构造方法则默认调用父类的构造方法
public Dog(String type, String name, int age) {
super(type, name, age);
}
//定义小狗的睡觉方法,实现输出:一只名为xx性别为xx的小狗,现在xx岁,它正在睡觉
public void sleep(){
System.out.printf("一只名为%s性别为%s的小狗,现在%d岁,它正在睡觉",this.getName(), sex, this.getAge());
}
}
Animal
public class Animal {
private String type;
private String name;
private int age;
public Animal(String type, String name, int age) {
this.type = type;
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Java继承和多态之对象类型的转换
把子类对象赋值给父类类型的变量(隐式转换,不用进行强制类型转换),被称为向上转型。
本质:父类的引用指向了子类的对象。
语法:
父类类型 引用名 = new 子类类型();
向下转型是指子类引用父类对象,就是将父类对象能转换成子类对象,这时需要满足两个条件:一是必须执行强制类型转换;二是必须确保父类对象是子类的一个实例,否则抛出异常。
语法:
子类类型 引用名 = (子类类型)父类引用
;
/**
* 使用对象类型的转换,根据编程提示,完成猫类和动物类的转换,以及彼此方法和属性的调用
*/
// 定义动物类,并初始化自身属性和方法
class Animal{
public String name = "动物";
public static String staticName = "可爱的动物";
public void eat() {
System.out.println("动物吃饭");
}
public static void staticEat() {
System.out.println("可爱的动物正在在吃饭");
}
}
// 定义猫类,该类继承动物类
class Cat extends Animal{
public String name = "猫";
public String str = "可爱的小猫";
public static String staticName = "我是喵星人";
public void eat() {
System.out.println("猫吃饭");
}
public static void staticEat() {
System.out.println("喵星人在吃饭");
}
public void eatMethod() {
System.out.println("猫喜欢吃鱼");
}
public static void main(String[] args) {
// 向上转型,把猫类对象赋值给动物类
Animal animal = new Cat();
// 向下转型,将动物类引用转换为猫类对象
Cat cat = (Cat) animal;
// 输出Animal类的name变量
System.out.println(animal.name);
// 输出Animal类的staticName变量
System.out.println(animal.staticName);
// 输出Cat类的eat()方法
animal.eat();
// 输出Animal类的staticEat()方法
animal.staticEat();
// 调用Cat类的str变量
System.out.println(cat.str);
// 调用Cat类的eatMethod()方法
cat.eatMethod();
}
}