charAt():根据索引获取当前字符串中字符
/**
* 统计一个字符在字符串中所在位置
*/
public static void main(String[] args) {
String str = "统计一个字符在字符串中的所有字位置";
int[] arr = {};
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c == '字'){
arr = Arrays.copyOf(arr, arr.length + 1);
arr[arr.length - 1] = i;
}
}
System.out.println(Arrays.toString(arr));
}
public static void main(String[] args) {
String str = "统计一个字符在字符串中的所有字位置";
int index = str.indexOf("字"); //找第一个
System.out.println(index);
}
public static void main(String[] args) {
String str = "统计一个字符在字符串中的所有字位置";
int index1 = str.lastIndexOf("字"); //找最后一个
System.out.println(index1);
}
length:无括号,属性;
length() :方法
数组的合并:
/**
* 数组的合并
* @param args
*/
public static void main(String[] args) {
int[] arr1 = {1,3,5};
int[] arr2 = {2,4,6};
for (int i = 0; i < arr1.length; i++) { //循环arr1,扩充arr2
arr2 = Arrays.copyOf(arr2,arr2.length + 1);
arr2[arr2.length - 1] = arr1[i];
}
System.out.println("Arrays.toString(arr2) = " + Arrays.toString(arr2));
}
合并方式2:两个数组都循环
/**
* 方式2
*/
public static void main(String[] args) {
int[] arr1 = {1,3,5};
int[] arr2 = {2,4,6};
int[] news = new int[arr1.length + arr2.length];
for (int i = 0; i < arr1.length; i++) { //循环arr1
news[i] = arr1[i];
}
for (int i = arr1.length; i < news.length; i++) { //循环arr2
news[i] = arr2[i - arr1.length];
}
Arrays.sort(news); //排序
System.out.println(Arrays.toString(news));
}
输出结果:[1, 2, 3, 4, 5, 6]
类和对象:
/**
* 面向对象
*/
public String name;
public int age;
public String sex;
public String hobby;
public static void main(String[] args) {
Student stu = new Student(); //将类转化为对象
//实例化的过程就是对象的创建过程
stu.name = "李四";
stu.age = 18;
stu.sex = "女";
stu.hobby = "绘画";
//调用
run(stu);
}
public static void run(Student stu) {
System.out.println(stu.name);
System.out.println(stu.age);
System.out.println(stu.sex);
System.out.println(stu.hobby);
}
public class Apple {
public String name; //声明
public void variety(){ //构造方法
System.out.println("这是一个品种为" + name + "的苹果");
}
public static void main(String[] args) {
Apple apple = new Apple(); //创建对象
apple.name = "黄元帅";
System.out.println(apple.name);
apple.variety(); //调用
}
}
描述类和对象以及他们之间的关系:
类:用属性和行为来描述一组相同事物的集合 ;类的具体体现就是对象(new 地址)
类是用来描述对象的,对象是类的具体实例。
全局变量存在堆中,可以不用初始化,new之后会默认初始化;
局部变量存在栈中,没有初始化功能,使用时必须初始化。
public class Tree {
public String a; //全局变量,可以不初始化
public String b = "哈哈";
public void show(){
int i; //局部变量,没有初始化,不能输出
int x = 10; //可以输出
System.out.println(x);
}
public static void main(String[] args) {
Tree tree = new Tree();
System.out.println(tree.a);
System.out.println(tree.b);
tree.show(); //非静态方法,必须要实例化对象调用(或者将show方法改为静态可以直接调用)
}
}
public class Student {
public String name;
public String no;
public double height;
public void study(){
System.out.println(name + "在学习");
}
public void eat(){
System.out.println(name + "在吃");
}
public void sleep(){
System.out.println(name + "在睡觉");
}
}
public class StudentTest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//数组
Student[] students = new Student[5];
for (int i = 0; i < 5; i++) {
Student stu = new Student(); //创建对象
//不能创建到外面,如果在外面,则五次都是相同的地址,后面的会将前面的覆盖
System.out.println("第" + (i+1) +"次输入的姓名");
stu.name = sc.next();
System.out.println("第" + (i+1) +"次输入的学号");
stu.no = sc.next();
System.out.println("第" + (i+1) +"次输入的身高");
stu.height = sc.nextDouble();
//stu.sleep();调用Student类中的方法
students[i] = stu; //存储信息
}
//System.out.println(students);//引用传递,输出的是内存地址
printStudent(students); //实参
}
public static void printStudent(Student[] s) { //形参
//System.out.println(Arrays.toString(s));//和上面的是同一个对象,内存地址相同
//输出之后是值,不会是内存地址
for (Student x: s){ //增强for循环 类型 每次取出的新名(新名):要循环的数组名(旧名)
System.out.println("{" + x.name + "," + x.no + "," + x.height + "}");
}
}
}