JAVA自学笔记(3)
JAVA的心动之旅
Day1 字符串String
1.0 字符串的特点以及创建一个字符串

public class Practice {//构建字符串的3+1种方法
public static void main(String[] args) {
//第一种
String one=new String();
System.out.println("输出的字符串为:"+one);
//第二种
char str[]={'A','B','C'};
String two=new String(str);
System.out.println("输出的字符串为:"+two);
//第三种
byte []bytes={97,98,99,100};
String three=new String(bytes);
System.out.println("输出的字符串为:"+three);
//+1种 直接构造 字符串常量不可改变
String four="HelloWorld";
System.out.println("输出的字符串为:"+four);
}
}
打印结果:
输出的字符串为:
输出的字符串为:ABC
输出的字符串为:abcd
输出的字符串为:HelloWorld
2.0 字符串的常量池

3.0 字符串的获取方法

public class Practice {
public static void main(String[] args) {
String one="Hello";
System.out.println("字符串的长度为:"+one.length());
String two="say ".concat(one);
System.out.println("字符串为:"+two);
for(int i=0;i<two.length();i++)
{
char ch=two.charAt(i);
System.out.print(ch+" ");
}
String three="o";
System.out.println("\n"+one.indexOf(three));
String four="h";
System.out.println(one.indexOf(four));
}
}
打印结果
字符串的长度为:5
字符串为:say Hello
s a y H e l l o
4
-1
4.0 字符串的比较 ==为地址的比较

5.0 字符串的截取

public class Practice {
public static void main(String[] args) {
String one="hellobts";
String two=one.substring(5);
System.out.println(two);
String three=one.substring(0, 5);
System.out.println(three);
}
}
打印结果:
bts
hello
6.0 字符串的转化方法

public class Practice {
public static void main(String[] args) {
String one="hellobts";
char a[]=one.toCharArray();
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
byte []bytes=one.getBytes();
for(int i=0;i<bytes.length;i++)
{
System.out.print(bytes[i]+" ");
}
System.out.println();
String two=one.replace("hello", "love");
System.out.println(two);
}
}
打印结果:
h e l l o b t s
104 101 108 108 111 98 116 115
lovebts
Day2 static 关键字
1.0 static 概述
一旦用了static 关键字,那么这样的内容不再属于对象自己,它是属于类的,所以凡是本类的对象,都共享一份。
2.0 static 修饰成员变量
public class Students {
private static int idcounter=0;//每当创建一个对象(new)计数器++
static String room;
private String name;
private int age;
private int id;
public Students() {
this.id=++idcounter;
}
public Students(String name, int age) {
this.name = name;
this.age = age;
this.id=++idcounter;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 Practice {
public static void main(String[] args) {
Students one=new Students("田小娟",22);
one.room="101教室";
Students two=new Students("徐穗珍",22);
System.out.println("姓名:"+one.getName()+" "+"年龄:"+one.getAge()+" 教室:"+one.room+
" 学号"+one.getId());
System.out.println("姓名:"+two.getName()+" "+"年龄:"+two.getAge()+" 教室:"+two.room+
" 学号"+two.getId());
}
}
3.0 static 修饰方法

4.0 静态代码块

Day3 与Arrays相识

1.0 简单的应用
import java.util.Arrays;
public class Practice {
public static void main(String[] args) {
int []num={3,89,45,235,43,79};
String str=Arrays.toString(num);
System.out.println(str);
Arrays.sort(num);
for(int i=0;i<num.length;i++)
{
System.out.print(num[i]+" ");
}
}
}
打印结果:
[3, 89, 45, 235, 43, 79]
3 43 45 79 89 235
2.0 字符串倒序
import java.util.Arrays;
public class Practice {
public static void main(String[] args) {
String str="aihfjsdfhuwefwnf";
//定义一个随机的字符串,并将字符串排序后倒序输出
//需将字符串先转化为字符数组,才能使用Arrays
char []chars=str.toCharArray();
Arrays.sort(chars);
for(int i=chars.length-1;i>=0;i--)
{
System.out.print(chars[i]+" ");//w w u s n j i h h f f f f e d a
}
}
}
Math类方法(百度)




JAVA自学笔记(3)的更多相关文章
- JAVA自学笔记09
JAVA自学笔记09 1.子类的方法会把父类的同名方法覆盖(重写) 2.final: 1)可修饰类.方法.变量 2)修饰类时:此时该类变为最终类,它将无法成为父类而被继承 3)修饰方法时:该方法将无法 ...
- JAVA自学笔记05
JAVA自学笔记05 1.方法 1)方法就是完成特定功能的代码块,类似C语言中的函数. 2)格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2,-){ 函数体; return ...
- JAVA自学笔记06
JAVA自学笔记06 1.二维数组 1)格式: ①数据类型[][]数组名 = new 数据类型[m][n]; 或 数据类型[]数组名[]=new 数据类型[m][n]; m表示这个二维数组有多少个一维 ...
- JAVA自学笔记04
JAVA自学笔记04 1.switch语句 1)格式:switch(表达式){ case 值1: 语句体1; break; case 值2: 语句体2; break; - default: 语句体n+ ...
- JAVA自学笔记07
JAVA自学笔记07 1.构造方法 1) 例如:Student s = new Student();//构造方法 System.out.println(s);// Student@e5bbd6 2)功 ...
- JAVA自学笔记10
JAVA自学笔记10 1.形式参数与返回值 1)类名作为形式参数(基本类型.引用类型) 作形参必须是类的对象 2)抽象类名作形参 需要该抽象类的子类对象,通过多态实现 3)接口名为形参 需要的是该接口 ...
- JAVA自学笔记13
JAVA自学笔记13 1.StringBuffer类 1)线程安全的可变字符序列 线程安全(即同步) 2)StringBuffer与String的区别:一个可变一个不可变 3)构造方法: ①publi ...
- JAVA自学笔记11
JAVA自学笔记11 1:Eclipse的安装 2:用Eclipse写一个HelloWorld案例,最终在控制台输出你的名字 A:创建项目 B:在src目录下创建包.cn.itcast C:在cn.i ...
- JAVA自学笔记14
JAVA自学笔记14 1.正则表达式 1)是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.其实就是一种规则.有自己的特殊应用 2)组成规则: 规则字符在java.util.rege ...
- JAVA自学笔记12
JAVA自学笔记12 1.Scanner 1)JDK5后用于获取用户的键盘输入 2)构造方法:public Scanner(InputStream source) 3)System.in 标准的输入流 ...
随机推荐
- 谷歌浏览器的F12用处及问题筛查笔记
在前端测试功能的时候,经常有些莫名其妙的错误,这个时候开发会说打开F12看一下吧,所以感觉这个开发者功能很有用,研究一下,做如下记录: Elements:左栏以DOM树形式查看网页源代码(HTML), ...
- vue element select多选回显
我们经常在使用 Element组件里面的 select多选 场景:添加账号的时候需要选择可见分公司(分公司为多选),添加成功之后可以编辑,需要回显添加时所提交的分公司 代码如下: 多选框: data( ...
- MySQL升级-CentOS6.8
在腾讯云购买的服务器自带的MySQL是5.1版本的,相对于最新版的5.7差了很多特性,在平时的项目练习中使用到了MySQL也会遇到一些奇葩的错误,很有必要升级到至少5.5版本以上. 步骤: 1.备份数 ...
- thinkphp下的Webshell&&php过D盾一句话
环境: Thinkphp 5.0.15 PHP version:7.0.12 WAF: D盾 ,安全狗 Thinkphp 采用 MVC 模式 核心:模块 -> 控制器 –> 方法 思路: ...
- 02_Java语法
1.注释 2.关键字 3.标识符 4.常量 5.变量 6.数据类型 7.数据类型转换 8.表达式 9.运算符 9.1算数运算符 9.2赋值运算符 9.3比较运算符 9.4逻辑运算符 9.5三元运算符 ...
- 补 第三场多校杭电 费用流 K Subsequence
K Subsequence 这个题目是这个人想吃东西,但是他每次吃的都是他的美味值都必须不递减,可以吃k次,问这个最大的美味值是多少. 这个是一个比较明显的费用流,建图也很好建,但是呢,这个题目卡sp ...
- Coursera课程笔记----Write Professional Emails in English----Week 4
Request and Apology Emails(Week 4) How to Write Request Emails Write more POLITELY & SINCERELUY ...
- 【大数据 Spark】利用电影观看记录数据,进行电影推荐
利用电影观看记录数据,进行电影推荐. 目录 利用电影观看记录数据,进行电影推荐. 准备 1.任务描述: 2.数据下载 3.部分数据展示 实操 1.设置输入输出路径 2.配置spark 3.读取Rati ...
- Redis 学习笔记(一) 字符串 SDS
SDS 简单动态字符串. SDS的结构: struct sdshdr{ int len;//记录BUF数组中已使用字节的数量 ,等于SDS所八寸字符串的长度 int free;//记录BUF数组中未使 ...
- 20184302 2019-2020-2 《Python程序设计》实验一报告
20184302 2019-2020-2 <Python程序设计>实验一报告 课程:<Python程序设计> 班级: 1843 姓名: 李新锐 学号:20184302 实验教师 ...