菜鸟的JavaSE学习之旅6

Scanner类

键盘输入数据到程序当中
引用类型的一般使用步骤:
1、导包

import 包路径.类名称

若需要使用的包和当前类位于同一个包下,则可以省略导包语句
只有java.lang包下的内容不需要导包
2、创建

类名称 对象名 = new 类名称()

3、使用

对象名.成员方法名()
import java.util.Scanner;
public class DemoScanner{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt(); 
		String str = sc.next();
		System.out.println("输入的数字是"+num+",输入的字符串是"+String);
	}
}

匿名对象

只有右边的对象,没有左边的名字和赋值运算符
new 类名称()
匿名对象只能使用唯一一次,下次再用需要创建一个新对象

Random类

用来生成随机数字

导包

import java.util.Random

创建

Random r = new Random();
Random r = new Random();
int num = r.nextInt(3);//代表[0,3),左闭右开
int num = r.nextInt(100)+1;//代表[1,100] 

使用

获取随机的int数字(范围是int所有范围)

public class DemoRandom{
	public static void main(String[] args){
		Random r = new Random();
		int num = r.nextInt();
		System.out.println(num);
	}
}

ArrayList类

长度可以改变
ArrayList

ArrayList<String> list = new ArrayList<>();
list.add("小赵");

常用方法

public boolean add(E e);//向集合中添加元素
public E get(int index);//从集合中获取元素,
public E remove(int index);//从集合中删除元素
public int size();//获取集合的尺寸长度,返回集合中元素的个数
public class DemoArrayList{
	public static void main(String[] args){
		ArrayList<String> list = new ArrayList<>();
		boolean success = list.add("小赵");//返回是否成功
		list.add("小王");
		list.add("小李");
		list.add("小陈");
		System.out.println(list);
		String name = list.get(2);
		String whoremoved = list.remove(3);//被删掉的元素
		int size = list.size();
	}
}

ArrayList集合存储基本数据类型

//基本类型和包装类
byte  Byte
short short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
//JDK1.5开始,自动装箱,自动拆箱
public class DemoArrayListBasic{
	public static void main(String[] args){
		ArrayList<String> list = new ArrayList<>();
		ArrayList<Integer> list1 = new ArrayList<>();
		list1.add(100);
		list1.add(200);
		int num1 = list1.get(1);
	}
}

练习题

生成6个1·33之间的随机整数,添加到集合并遍历集合

1、存储6个数字,创建集合Integer
2、产生随机数Random
3、for循环r.nextInt(int n)整体加1
4、数字添加到集合中add
5、遍历集合:for、size、get

public class DemoArrayListBasic{
	public static void main(String[] args){
		ArrayList<Integer> list = new ArrayList<>();
		Random r = new Random();
		for(int i = 0; i < 6; i++){
			int num = r.nextInt(33)+1;
			list.add(num);
		}
		//遍历
		for(int i=0; i<list.size();i++){
			System.out.println(list.get(i));
		}
	}
}

存储自定义类型:自定义4个学生对象,添加到集合并遍历

1、自定义Student学生,四个部分
2、创建一个集合,用来存储学生对象,泛型,
3、根据类创建4个集合对象
4、学生对象添加至集合中:add
5、遍历集合:for、size、get

//学生类
public class Student{
	private String name;
	private int age;
	public Student(){
	}
	public Student(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;
	}
}
public class DemoArrayListStudent{
	public static void main(String[] args){
		ArrayList<Student> stu = new ArrayList<>();
		Student one = new Student("小王",20);
		Student two = new Student("小李",10);
		Student three = new Student("小陈",25);
		Student four = new Student("小张",34);
		stu.add(one);
		stu.add(two);
		stu.add(three);
		stu.add(four);
		//遍历
		for(int i=0; i<list.size();i++){
			Student s = list.get(i);
			System.out.println("姓名:" + s.getName() + ",年龄:" + s.getAge());
		}
	}
}

String

java.lang.String类代表字符串

字符串特点:

字符串的内容用不可变,可以共享使用
字符串效果上相当于是char[]字符数组,底层原理是byte[]字节数组

常见的创建方法

public String()
public String(char[] array);
public String(byte[] array);

字符串常量池

程序当中直接写上的双引号字符串,就在常量池中
在这里插入图片描述

字符串比较

// ==是进行对象的地址值比较
//public boolean equals(Object obj):参数可以是任何对象,只有参数是一个并内容相同才是true
//任何对象都能用Object进行接收;equals方法具有对称性a.equals(b)和b.equals(a)效果一样
public class DemoStringEquals{
	public static void main(String[] args){
		String str1 = "Hello";
		String str2 = "Hello";
		char[] charArray = {'H', 'e', 'l', 'l', 'o'};
		String str3 = new String(charArray);
		System.out.println(str1.equals(str2));
		System.out.println(str2.equals(str3));
		System.out.println(str1.equals("Hello"));
	}
}

字符串常用方法

public int length();//获取字符串中含有的字符个数,字符串长度
public String concat(String str);//将当前字符串和参数字符串拼接成新的字符串
public char charAt(int index);//获取指定索引位置的单个字符(索引从0开始)
public int indexOf(String str);//查找参数字符串在本字符串中首次出现的位置
public class DemoStringGet{
	public static void main(String[] args){
		//获取字符串长度
		int length = "abfjdkfgjj".length();
		System.out.println("字符串的长度是:" + length);
		//拼接
		String str1 = "Hello";
		String str2 = "World";
		String str3 = str1.concat(str2);
		System.out.println(str1);//Hello
		System.out.println(str2);//World
		System.out.println(str3);//HelloWorld
		//获取指定索引位置的单个字符
		char ch = "Hello".chatAt(1);//e
		//查找参数字符串在本字符串中首次出现的位置
		String s1 = "HelloWorld";
		int index = s1.indexOf("llo");//2
		int index1 = s1.indexOf("abc");//-1
	}
}

字符串截取

public String substring(int index);//截取从参数位置到字符串末尾
public String substring(int begin, int end);//截取从begin开始到end结束,左闭右开区间
public class DemoSubString{
	public static void main(String[] args){
		String str1 = "HelloWorld";
		String str2 = str1.substring(5);
		String.out.println(str1);//HelloWorld
		String.out.println(str2);//World
		String str2 = str3.substring(4,7);//oWo
	}
}

字符串转换

public char[] toCharArray();//将当前字符串拆分成字符数组作为返回值
public byte[] getBytes();//获得当前字符串底层的字节数组
public String replace(CharSequence oldString, CharSequence newString);//替换
public class DemoStringConvert{
	public static void main(String[] args){
		char[] chars = "Hello".toCharArray();
		String.out.println(chars[0]);//H
		System.out.println(chars.length);//5
		byte[] bytes = "abc".getBytes();
		for(int i=0;i<bytes.length;i++){
			System.out.println(bytes[i]);//97 98 99
		}
		String str1 = "hello";
		String str2 = str1.replace("o","*");//hell*
	}
}

字符串分割

public string[] split(String regex);
//参数是正则表达式,英文句点"."需要写成"\\."
public class DemoStringSplit{
	public static void main(String[] args){
		String str1 = "aaa,bbb,ccc";
		String[] array1 = str1.split(",");//看见逗号切一刀
		System.out.println(array1[0]);//aaa
	}
}

static静态关键字

只在类中保存唯一一份,所有本类对象共享同一份;一旦用了static关键字,这样的内容不再属于对象,而是属于类,凡是本类的对象,都共享同一份
使用static修饰成员方法,就成为了静态方法,静态方法不属于对象,而是属于类,如果没有static关键字,那么必须首先创建对象,然后通过对象才能使用,静态方法可以直接通过类名来调用
静态不能直接访问非静态,因为在内存中,先有静态内容,后有非静态内容
静态方法不能用this

静态方法:类名称.静态方法名
静态变量:类名称.静态变量

在这里插入图片描述

静态代码块

public class 类名称{
	static{
		//静态代码块的内容
	}
}

特点:当第一次用到本类,静态代码块执行唯一一次
静态内容优先于非静态,静态代码块比构造方法先执行
静态代码块用来一次性地对静态成员变量进行赋值

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值