java
import java.util.ArrayList;
public class Solution {
//将字符串转化为整数
public static void main(String[] args) {
ArrayList<String> list=new ArrayList();
list.add("aaa");
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("ddd");
list.add("eee");
String result=list.set(3,"aaa");
System.out.println(result);//返回被修改的元素
for (int i = 0; i < list.size(); i++) {
String str=list.get(i);
System.out.println(str);
}
}
}
java
import java.util.ArrayList;
public class Solution {
//将字符串转化为整数
public static void main(String[] args) {
//1.创建集合
ArrayList<Student> list=new ArrayList<>();
//2.创建学生对象
Student s1=new Student("lajfl",2);
Student s2=new Student("fsda",4);
//3.添加元素
list.add(s1);
list.add(s2);
//4.循环遍历
for (int i = 0; i < list.size(); i++) {
Student st=list.get(i);
System.out.println(st);
}
}
}
是自己写的,并没有进行一些特殊的处理,所以直接打印时会打印首地址
java
import java.util.ArrayList;
public class Solution {
//将字符串转化为整数
public static void main(String[] args) {
//1.创建集合
ArrayList<Student> list=new ArrayList<>();
//2.创建学生对象
Student s1=new Student("lajfl",2);
Student s2=new Student("fsda",4);
//3.添加元素
list.add(s1);
list.add(s2);
//4.循环遍历
for (int i = 0; i < list.size(); i++) {
Student st=list.get(i);
System.out.println(st.getAge()+""+st.getName());
}
}
}
添加用户对象,并判断是否存在
java
import java.util.ArrayList;
public class Javabean {
public static void main(String[] args) {
ArrayList<Phone> list=new ArrayList<>();
Phone p1=new Phone("1a","12345a","jf");
Phone p2=new Phone("1b","2345a","lajf");
Phone p3=new Phone("1c","345a","jlf");
list.add(p1);
list.add(p2);
list.add(p3);
int result=User(list,"1c");
System.out.println(result);
}
//调用方法根据id查找对应的用户信息
public static int User(ArrayList<Phone> list,String id){
for (int i = 0; i < list.size(); i++) {
String id1 =list.get(i).getId();
if(id1==id){
return i;
}
}
return -1;
}
}
添加手机对象,并返回要求的数据
java
import java.util.ArrayList;
public class Javabean {
public static void main(String[] args) {
//1.创建一个集合
ArrayList<Phone> list=new ArrayList<>();
//2.创建对象
Phone p1=new Phone("小米",1000);
Phone p2=new Phone("苹果",8000);
Phone p3=new Phone("锤子",2900);
//3.添加对象
list.add(p1);
list.add(p2);
list.add(p3);
ArrayList<Phone> lowlist=judge(list);
for (int i = 0; i < lowlist.size(); i++) {
Phone phone1=lowlist.get(i);
System.out.println(phone1.getBrand()+" "+ phone1.getPrice());
}
}
//方法:将《3000的手机返回
public static ArrayList<Phone> judge(ArrayList<Phone> list){
ArrayList<Phone> lowlist=new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
Phone phone=list.get(i);
int price1=phone.getPrice();
if(price1<3000){
lowlist.add(phone);
}
}
return lowlist;
}
关键点:怎么返回多个对象?
可以创建一个集合储存要返回的元素
后返回这个集合
多态练习
行为:eat
属性:年龄,颜色
1.定义狗类
属性:年龄,颜色
行为:eat(String something)(something表示吃的东西)
看家lookHome方法(无参数)
2.定义猫类
属性:年龄,颜色
行为:
eat(String something)方法(something表示吃的东西)
逮老鼠catchMouse方法(无参数)
3.定义Person类//饲养员
属性:姓名,年龄
行为:keepPet(Dog dog,String something)方法
功能:喂养宠物狗,something表示喂养的东西
行为:keepPet(Cat cat,String something)方法
功能:喂养宠物猫,something表示喂养的东西
生成空参有参构造,set和get方法
4.定义测试类(完成以下打印效果):
keepPet(Dog dog,String somethind)方法打印内容如下:
年龄为30岁的老王养了一只黑颜色的2岁的狗
2岁的黑颜色的狗两只前腿死死的抱住骨头猛吃
keepPet(Cat cat,String somethind)方法打印内容如下:
年龄为25岁的老李养了一只灰颜色的3岁的猫
3岁的灰颜色的猫眯着眼睛侧着头吃鱼
5.思考:1.Dog和Cat都是Animal的子类,以上案例中针对不同的动物,定义了不同的keepPet方法,过于繁琐,能否简化,并体会简化后的好处?
2.Dog和Cat虽然都是Animal的子类,但是都有其特有方法,能否想办法在keepPet中调用特有方法?
Package src
class animals;
java
public class Animals {
private String color;
private int age;
public Animals(String color, int age) {
this.color = color;
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void eat(String something){
System.out.println("小动物吃"+something);
}
}
Package src
class Cat;
java
public class Cat extends Animals{
public Cat(String color, int age) {
super(color, age);
}
@Override
public void eat(String something) {
System.out.println(getAge()+"岁的"+getColor()+"的猫眯着眼睛侧着头吃"+something);
}
public void catchMouse(){
System.out.println("捉老鼠");
}
}
Package src
class Cat;
java
public class Dog extends Animals{
public Dog(String color, int age) {
super(color, age);
}
@Override
public void eat(String something) {
System.out.println(getAge()+"岁的"+getColor()+"颜色的狗两只前腿死死的抱住"+something+"猛吃");
}
}
Package src
class Cat;
java
public class Person {
/*定义Person类//饲养员
属性:姓名,年龄
行为:keepPet(Dog dog,String something)方法
功能:喂养宠物狗,something表示喂养的东西
行为:keepPet(Cat cat,String something)方法
功能:喂养宠物猫,something表示喂养的东西
生成空参有参构造,set和get方法*/
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
java
public void keepPet(Dog dog, String something){
System.out.println("年龄为"+getAge()+"岁的"+getName()+"养了一只"+dog.getColor()+"颜色的"+dog.getAge()+"的猫");
dog.eat(something);
}
public void keepPet(Cat cat,String something){
System.out.println("年龄为"+getAge()+"岁的"+getName()+"养了一只"+cat.getColor()+"颜色的"+cat.getAge()+"岁的猫");
cat.eat(something);
}
优化:
思考:1.Dog和Cat都是Animal的子类,以上案例中针对不同的动物,定义了不同的keepPet方法,过于繁琐,能否简化,并体会简化后的好处?
2.Dog和Cat虽然都是Animal的子类,但是都有其特有方法,能否想办法在keepPet中调用特有方法?*/
java
public void keepPet(Animals animal,String something){
if(animal instanceof Dog d){
System.out.println("年龄为"+getAge()+"岁的"+getName()+"养了一只"+animal.getColor()+"颜色的"+animal.getAge()+"岁的猫");
d.eat(something);}
else if(animal instanceof Cat c){
System.out.println("年龄为"+getAge()+"岁的"+getName()+"养了一只"+animal.getColor()+"颜色的"+animal.getAge()+"岁的狗");
c.eat(something);
}else{
System.out.println("没有此动物");
}
}
Package src
class Cat;
java
public class Test {
public static void main(String[] args) {
Person p=new Person("老王",78);
Cat c=new Cat("blue",2);
p.keepPet(c,"胡萝卜");
Person p1=new Person("老李",12);
Dog dog=new Dog("white",15);
p.keepPet(dog,"肉");
}
}