break以及continue
package com.iteasyup.javase;
public class break关键字 {
public static void main(String[] args) {
for (int i = 1; i < 11; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
}
}
collection的使用
package com.iteasyup.javase;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionTest {
public static void main(String[] args) {
Collection<Integer> coll = new ArrayList<>();
coll.add(10);
coll.add(20);
coll.add(30);
coll.add(30);
coll.remove(20);
coll.remove(10);
coll.add(40);
int size = coll.size();
System.out.println(size);
System.out.println(coll.contains(50));
System.out.println(coll.isEmpty());
for (Integer a : coll) {
System.out.println(a);
}
System.out.println(coll);
Iterator<Integer> iterator = coll.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
do while循环以及while循环的区别
package com.iteasyup.javase;
public class 循环之dowhile {
public static void main(String[] args) {
int i = 1;
do {
if (i * 2 + (35-i) * 4 == 94) {
System.out.println("鸡有:" + i + "只;" + "兔有:" + (35 - i) + "只");
}
i++;
} while (i < 36);
}
}
foreach循环
package com.iteasyup.javase;
public class foreac循环 {
public static void main(String[] args) {
int[] a = {11,22,33,44,55};
for (int i : a) {
System.out.println(i);
}
String[] b = {"da","ji","ge","you","are","beautiful"};
for (String bb : b) {
System.out.println(bb);
}
}
}
for死循环
package com.iteasyup.javase;
import java.util.Scanner;
public class for死循环 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
for (; ; ) {
System.out.println("请选择:1.VIP会员 2.普通会员 3.删除会员信息 4.添加会员信息 5.退出");
int choose = in.nextInt();
if (choose == 1) {
System.out.println("VIP会员");
}
if (choose == 2) {
System.out.println("普通会员");
}
if (choose == 3) {
System.out.println("删除会员信息");
}
if (choose == 4) {
System.out.println("添加会员信息");
}
if (choose == 5) {
System.out.println("退出系统");
break;
}
}
}
}
lambda运算符的使用
package com.iteasyup.javase;
public class T01 {
public int f1(Math math, int a, int b) {
return math.js(a, b);
}
public static void main(String[] args) {
T01 t01 = new T01();
Math math = (a, b) -> a + b;
int f1 = t01.f1(math, 3, 5);
System.out.println(f1);
}
}
package com.iteasyup.javase;
public interface Compare {
String compareTo(String s1, String s2);
}
package com.iteasyup.javase;
public class TestCompare {
public void f1(Compare compare, String s1, String s2) {
System.out.println(compare.compareTo(s1, s2));
}
public static void main(String[] args) {
Compare compare = (s1, s2) -> {
if (s1.length() - s2.length() > 0) {
return s1;
}
else {
return s2;
}
};
new TestCompare().f1(compare, "liuji", "dajige");
}
}
List的使用
package com.iteasyup.javase;
import java.util.ArrayList;
import java.util.List;
public class TestList {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("叶秋");
list.add("叶修");
list.add("陈果");
list.add("叶秋");
list.remove(2);
list.set(1, "a");
System.out.println(list.get(0));
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println(list);
}
}
main以及syso输出
package com.iteasyup.javase;
public class Test1 {
public static void main(String[] args) {
System.out.println(10);
System.out.println("hello world");
System.out.println();
}
}
map的使用
package com.iteasyup.javase;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class TestMap {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(1, "小王");
map.put(2, "小黑");
map.put(3, "小白");
map.put(null, null);
map.remove(2);
map.remove(3, "小黑");
map.put(1, "效率");
System.out.println(map.get(1));
Set<Integer> keys = map.keySet();
for (Integer integer : keys) {
System.out.println(map.get(integer));
}
Collection<String> values = map.values();
for (String string : values) {
System.out.println(string);
}
System.out.println(map);
}
}
Reader、Writer+Buff
package com.iteasyup.javase;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class Test9 {
public static void main(String[] args) throws IOException {
File file = new File("D:\\xiaoli.txt");
File file2 = new File("D:\\test6.txt");
Reader reader = new FileReader(file);
Writer writer = new FileWriter(file2);
BufferedReader b = new BufferedReader(reader);
BufferedWriter w = new BufferedWriter(writer);
String s = "";
while ((s = b.readLine()) != null) {
System.out.println(s);
s = s.replace("小", "大");
System.out.println(s);
w.write(s + "\r\n");
}
w.close();
b.close();
writer.close();
reader.close();
}
}
Reader+Buff
package com.iteasyup.javase;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class TestBufferedReader {
public static void main(String[] args) throws IOException {
File file = new File("D:\\aa.txt");
Reader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
String s1 = "";
while ((s1 = br.readLine()) != null) {
System.out.println(s1);
}
br.close();
reader.close();
}
}
Reader的使用
package com.iteasyup.javase;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class TestReader {
public static void main(String[] args) {
File file = new File("D:\\aa.txt");
Reader reader = null;
try {
reader = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int result = 0;
try {
if (reader != null) {
while((result = reader.read()) != -1) {
System.out.println((char)result);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Set的使用及其与List之间的区别
package com.iteasyup.javase;
import java.util.HashSet;
import java.util.Set;
public class TestSet {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("aa");
set.add("bb");
set.add("cc");
set.add("dd");
for (String string : set) {
System.out.println(string);
}
System.out.println(set);
}
}
Set下面的update和delete方法
package com.jihezonghlianxi.javase;
public class StudentCheckSet implements StudentCheck{
@Override
public void addStudent(Student student) {
Modle.SET.add(student);
}
@Override
public void deleteStudent(int id) {
Student s1 = null;
for (Student student : Modle.SET) {
if (student.getId() == id) {
s1 = student;
}
}
Modle.SET.remove(s1);
}
@Override
public void updateStudent(int id, Student student) {
deleteStudent(id);
addStudent(student);
}
}
switch语句
package com.iteasyup.javase;
import java.util.Scanner;
public class Testswitch语句 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("请输入一个数字:");
int num = in.nextInt();
switch (num) {
case 1:
case 3:
case 5:
System.out.println("周一三五吃方便面");
break;
case 2:
case 4:
case 6:
System.out.println("周二四六喝白开水");
break;
case 7:
System.out.println("周日吃米饭");
break;
default:
System.out.println("输入错误,只能输入1-7的数字");
break;
}
}
}
while循环
package com.iteasyup.javase;
public class 循环之while {
public static void main(String[] args) {
int sum = 1;
int i = 0;
while (i < 6) {
sum = sum + i;
System.out.println(sum);
i++;
}
}
}
Writer的使用
package com.iteasyup.javase;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class TestWriter {
public static void main(String[] args) {
File file = new File("D:\\aa.txt");
Writer writer = null;
try {
writer = new FileWriter(file);
String s1 = "小明说小利真丑";
writer.write(s1);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
返回值
package com.iteasyup.javase;
public class ReturnType {
public static void main(String[] args) {
int buywater = ReturnType.buywater(100);
System.out.println(buywater);
}
public static int buywater(int money) {
money = money - 10;
return money;
}
}
方法重载方便输入
package com.iteasyup.javase;
public class Test {
public int max(int a, int b) {
return a > b ? a : b;
}
public double max(double a, double b) {
return a > b ? a : b;
}
}
package com.iteasyup.javase;
public class T01 {
public static void main(String[] args) {
Test test = new Test();
int max = test.max(10, 29);
System.out.println(max);
double max2 = test.max(2.3, 5.6);
System.out.println(max2);
}
}
获取当前系统时间 日期 Date
package com.iteasyup.dao;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TestDate {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = now.format(date);
System.out.println(time);
}
}
继承关系的上下转型
package com.iteasyup.com;
public class MasterTest {
public static void main(String[] args) {
Master master = new Master();
Animal animal = new Cat();
animal.eat();
Cat cat = (Cat)animal;
cat.catchMoue();
}
}
接口的结构
package com.iteasyup.javase;
public interface Lock {
FangDaoMen fangDaoMen = new FangDaoMen();
public static final int A = 10;
void lock();
void unlock();
}
进程休眠的sleep和wait的区别以及死锁
package com.iteasyup.javase;
public class T1 extends Thread {
@Override
public void run() {
synchronized ("aa") {
System.out.println("1");
synchronized ("bb") {
System.out.println(2);
}
}
}
}
package com.iteasyup.javase;
public class T2 extends Thread{
@Override
public void run() {
synchronized ("aa") {
System.out.println(3);
synchronized ("bb") {
System.out.println(4);
}
}
}
}
package com.iteasyup.javase;
public class Buyer extends Thread{
@Override
public void run() {
for(;;) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Market.goods == 0) {
System.out.println("已经没有货物啦!");
break;
}
Market.goods -= 5;
System.out.println("买家买走货物,货物剩余" + Market.goods + "份");
}
}
}
package com.iteasyup.javase;
public class T2 extends Thread {
@Override
public void run() {
synchronized ("bb") {
System.out.println(3);
synchronized ("aa") {
System.out.println(4);
}
}
}
}
package com.iteasyup.javase;
public class T1 extends Thread{
@Override
public void run() {
synchronized ("aa") {
System.out.println(1);
synchronized ("bb") {
System.out.println();
}
}
}
}
可变形参的用法
package com.iteasyup.javase;
public class Test {
public void f1(String...strings) {
for (String string : strings) {
System.out.println(string);
}
}
public static void main(String[] args) {
Test test = new Test();
test.f1("aa", "bb", "cc", "dd");
}
}
控制台接值
package com.iteasyup.javase;
import java.util.Scanner;
public class 控制台接值 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("请输入一个整数:");
int a = in.nextInt();
System.out.println(a);
System.out.println("请输入一个小数:");
double b = in.nextDouble();
System.out.println(b);
System.out.println("请输入一个姓名:");
String name = in.next();
System.out.println(name);
}
}
快速排序算法的Java代码实现
package Ji1;
public class QuickSort {
public static void quickSort(int[] arr,int low,int high){
int i,j,temp,t;
if(low>high){
return;
}
i=low;
j=high;
temp = arr[low];
while (i<j) {
while (temp<=arr[j]&&i<j) {
j--;
}
while (temp>=arr[i]&&i<j) {
i++;
}
if (i<j) {
t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
arr[low] = arr[i];
arr[i] = temp;
quickSort(arr, low, j-1);
quickSort(arr, j+1, high);
}
public static void main(String[] args){
int[] arr = {10,7,2,4,7,62,3,4,2,1,8,9,19};
quickSort(arr, 0, arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
拦截器
package com.iteasyup.fifth.advice;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class CommonIntercepter implements HandlerInterceptor{
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("preHandle execute...");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("postHandle execute...");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("afterCompletion execute...");
}
}
需要在springmvc.xml中实例化该类,然后配置拦截路径
<mvc:interceptors>
<mvc:interceptor>
<!-- a
利用迭代器从collection中取出数据的方式
Iterator<String> it = coll.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
利用方法给当前进程命名以及取名和解决并发问题
package com.qita.javase;
public class TestTicket implements Runnable{
int ticket = 100;
@Override
public void run() {
for(;;) {
ticket();
}
}
public synchronized void ticket() {
if (ticket >= 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "剩余票数为" + ticket-- + "张");
}
}
public static void main(String[] args) {
Runnable r1 = new TestTicket();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r1);
Thread t3 = new Thread(r1);
t1.setName("售票窗口一:");
t2.setName("售票窗口二:");
t3.setName("售票窗口三:");
t1.start();
t2.start();
t3.start();
}
}
利用面向对象对数组进行赋值
package com.iteasyup.javase;
public class TestArray {
int[] arr = new int[3];
public void addArr(int a, int b , int c) {
arr[0] = a;
arr[1] = b;
arr[2] = c;
}
public void selectArr() {
for (int i : arr) {
System.out.println(i);
}
}
public static void main(String[] args) {
TestArray s1 = new TestArray();
s1.addArr(4, 56, 2);
s1.selectArr();
}
}
两种利用进程休眠打印时间的方式
package com.iteasyup.javase;
public class Time implements Runnable {
@Override
public void run() {
for(int i = 0;;i++) {
System.out.println("已经过了"+ i/(3600 * 24 * 30 * 12) + "年" + i / (3600 * 24 * 30) % 13 + "月" + i / (3600 * 24) % 31 + "天" + i / 3600 % 25 + "时"+ i / 60 % 60 +"分" + i % 60 + "秒");
}
}
}
枚举类
package com.iteasyup.fifth.test;
import lombok.Getter;
@Getter
public enum Season {
SPRING("温暖", "春暖花开"),
SUMMER("炎热", "烈日炎炎"),
AUTUMN("凉爽", "秋高气爽"),
WINTER("寒冷", "白雪皑皑");
public String temporature;
private String description;
private Season(String temporature, String description) {
this.temporature = temporature;
this.description = description;
}
}
面试可能会问到的一些问题
package com.iteasyup.javase;
public class Test {
public static void main(String[] args) {
class T01{
int a = 10;
public void f22() {
System.out.println(a);
}
}
String s1 = "Hello";
String s2 = new String("java");
System.out.println(s2);
}
}
面向对象的一般方法调用和成员变量的赋值
package com.iteasyup.javase;
public class Student {
String name;
int no;
int age;
public void study() {
System.out.println("好好学习");
}
public void showStudentLife() {
System.out.println("逃课");
System.out.println("挂科");
System.out.println("谈恋爱");
}
}
package com.iteasyup.javase;
public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "刘吉";
s1.age = 22;
s1.no = 1001;
s1.study();
System.out.println(s1.name);
System.out.println(s1.age);
System.out.println(s1.no);
Student s2 = new Student();
s2.name = "珠兰";
s2.age = 10;
s2.showStudentLife();
System.out.println("该学生的姓名为" + s2.name + ",她的年龄是" + s2.age + "岁");
System.out.println(s2.no);
}
}
全部数据类型以及注释方式
package com.iteasyup.javase;
public class 数据类型 {
public static void main(String[] args) {
int a = 10;
int b = 20;
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println(a);
System.out.println(b);
System.out.println(10 >> 2);
}
}
全局变量和局部变量
package com.iteasyup.javase;
public class Person {
String name;
int age;
String sex;
public void changeName(String n) {
name = n;
}
public void addAge() {
age += 18;
}
public void showPerson() {
System.out.println("此人姓名为:" + name + ";年龄:" + age + "岁;性别为:" + sex);
}
public static void main(String[] args) {
Person p1 = new Person();
Person p2 = new Person();
p1.sex = "男";
p1.changeName("刘吉");
p1.addAge();
p1.showPerson();
p2.showPerson();
}
}
如何从控制台获得数字定义数组
package com.iteasyup.javase;
import java.util.Scanner;
public class 第二种定义数组的方式 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int[] arr = new int[5];
for (int i = 0; i < arr.length; i++) {
System.out.println("请输入第" + (i + 1) + "个数:");
arr[i]= in.nextInt();
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
如何递归输出1-5
public static void main(String[] args) {
m1(1);
}
public static void m1(int i) {
System.out.println(i);
i++;
if (i == 6) {
return;
}
m1(i);
}
如何对set里面的数据进行排序
package com.iteasyup.javase;
import java.util.Set;
import java.util.TreeSet;
public class TestTreeSet {
public static void main(String[] args) {
Set<Student> set = new TreeSet<>();
Student s1 = new Student("小红", 21);
Student s2 = new Student("小白", 24);
Student s3 = new Student("小黑", 56);
Student s4 = new Student("小花", 29);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
System.out.println(set);
}
}
package com.iteasyup.javase;
public class Student implements Comparable<Student> {
String name;
int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student o) {
return this.age - o.age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
如何获得一个整数的个位、十位、千位的数字
package com.iteasyup.javase;
public class 取余数 {
public static void main(String[] args) {
System.out.println(5 % 3);
int a = 12345;
System.out.println(a % 10);
System.out.println(a / 10 % 10);
System.out.println(a / 100 % 10);
System.out.println(a / 1000 % 10);
System.out.println(a / 10000 % 10);
}
}
如何获取1—100中所有的素数
package com.iteasyup.javase;
public class 循环拓展题5 {
public static void main(String[] args) {
for (int i = 1; i < 101; i++) {
int s = 0;
for (int j = 1; j < i + 1; j++) {
if (i % j == 0) {
++s;
}
}
if (s == 2) {
System.out.println("素数有:"+ i);
}
}
}
}
如何进行冒泡排序以及如何把一个数组中符合要求的数字转移到另一个数组
package com.iteasyup.javase;
import java.util.Scanner;
public class 扩展题唱歌比赛 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int[] a = new int[6];
for (int i = 0; i < a.length; i++) {
System.out.println("请输入第" + (i + 1) + "评委成绩:");
a[i] = in.nextInt();
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length - 1; j++) {
int temp = 0;
if (a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (int i = 0; i < a.length; i++) {
System.out.println("a的数组:" + a[i]);
}
int[] b = new int[a.length - 2];
int j = 0;
for (int i = 0; i < a.length; i++) {
if (i != 0 && i != a.length - 1) {
b[j] = a[i];
j++;
}
}
for (int i = 0; i < b.length; i++) {
System.out.println("b数组:" + b[i]);
}
double sum = 0;
for (int i = 0; i < b.length; i++) {
sum = sum + b[i];
}
System.out.println("选手的平均成绩为:" + sum/b.length + "分");
另一种冒泡:
int i;
int j;
int a[] = {5, 9, 6, 8, 7};
for (i = 0; i < a.length - 1; i++) {
int k = i;
for (j = i; j < a.length; j++) {
if (a[j] < a[k]) {
k = j;
}
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
for (i = 0; i < a.length; i++) {
System.out.println(a[i] + "");
System.out.println();
}
}
}
如何进行字符串之间的比较
package com.iteasyup.javase;
import java.util.Scanner;
public class 条件分支语句 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("请输入学生成绩:");
double score = in.nextDouble();
if (score >= 90 && score <= 100) {
System.out.println("优秀");
}else if (score >= 80 && score < 90) {
System.out.println("良好");
}else if (score >= 60 && score < 80) {
System.out.println("及格");
}else if (score >= 0 && score <60) {
System.out.println("不及格");
}else {
System.out.println("成绩必须在0-100之间");
}
}
}
如何让最后一个被打印的数后面没有逗号或其它字符
package com.iteasyup.javase;
public class 数组程序5 {
public static void main(String[] args) {
int[] a = {2,2,5,5,3};
for (int i = 0; i < a.length; i++) {
if (i == a.length - 1) {
System.out.print(a[i]);
}else {
System.out.print(a[i] + ",");
}
}
}
}
public static void f1(String s) {
String[] strings = s.split(",");
int count = 0;
for (int i = 0; i < strings.length; i++) {
int a = Integer.valueOf(strings[i]);
if (a % 2 != 0) {
count++;
if (count == 1) {
System.out.print(a);
}else {
System.out.print("," + a);
}
}
}
}
如何在保证安全性的前提下对成员变量进行赋值
package com.iteasyup.javase;
public class User {
String cardNo;
private double money;
String password;
public void setMoney(double money) {
if (money >= 0) {
this.money = money;
}
else {
System.out.println("输入错误");
}
}
public double getMoney() {
return money;
}
}
package com.iteasyup.javase;
public class UserTest {
public static void main(String[] args) {
User user = new User();
user.cardNo = "123456789";
user.setMoney(-1000);
user.password = "1qaz";
System.out.println(user.cardNo);
System.out.println(user.getMoney());
System.out.println(user.password);
}
}
如何在使用有参构造方法的时候,从控制台接值
package com.lingyige.javase;
public class Student {
int no;
int age;
String name;
public Student(int no, int age, String name) {
this.no = no;
this.age = age;
this.name = name;
}
public void showStudent() {
System.out.println("这个学生名字叫做:" + name + ",年龄为:" + age + ",学号为:" + no);
}
}
package com.lingyige.javase;
import java.util.Scanner;
public class StudentTest {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("请输入编号:");
int no = in.nextInt();
System.out.println("请输入年龄:");
int age = in.nextInt();
System.out.println("请输入姓名:");
String name = in.next();
Student student = new Student(no, age, name);
student.showStudent();
}
}
三目运算符
package com.iteasyup.javase;
import java.util.Scanner;
public class 三目运算符 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("请输入第一个值:");
int a = in.nextInt();
System.out.println("请输入第二个值:");
int b = in.nextInt();
System.out.println("请输入第三个值:");
int c = in.nextInt();
System.out.println((a > b ? a : b) > c ? (a > b ? a : b) : c);
}
}
删除list中重复的元素
public static List removeDuplicate(List list) {
for (int i = 0; i < list.size() - 1; i ++ ) {
for (int j = list.size() - 1; j > i; j --) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
return list;
}
设计模式:单例模式
package com.jihezonghlianxi.javase;
public class User {
String accont;
String pasword;
private User() {
}
private static final User USER = new User();
public static User getUser() {
return USER;
}
}
package com.jihezonghlianxi.javase;
public class UserTest {
public static void main(String[] args) {
User user = User.getUser();
user.accont = "a";
user.pasword = "b";
User user1 = User.getUser();
user1.accont = "a";
user1.pasword = "b";
}
}
设计模式:适配器模式
package com.jihezonghlianxi.javase;
public interface TestDao {
void f1();
void f2();
void f3();
void f4();
void f5();
}
package com.jihezonghlianxi.javase;
public class TestDaoImpl implements TestDao {
@Override
public void f1() {
}
@Override
public void f2() {
}
@Override
public void f3() {
}
@Override
public void f4() {
}
@Override
public void f5() {
}
}
package com.jihezonghlianxi.javase;
public class TestChild extends TestDaoImpl{
@Override
public void f1() {
super.f1();
}
@Override
public void f2() {
super.f2();
}
}
输出斐波那契数列
利用数组和for循环输出斐波那契数列
package com.iteasyup.javase;
import java.util.Scanner;
public class 斐波那契数列 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
}
}
package com.iteasyup.javase;
public class Fibonacci {
public static void f1(int[] a, int i) {
a[i] = a[i - 1] + a[i - 2];
System.out.println(a[i]);
i++;
if (i > 1000) {
return;
}
f1(a, i);
}
public static void main(String[] args) {
int[] a = new int[10000];
a[0] = 1;
a[1] = 1;
int i = 2;
f1(a,i);
}
}
输出流的使用
package com.iteasyup.javase;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TestOutputStream {
public static void main(String[] args) throws IOException {
File file = new File("D:\\aa.txt");
OutputStream os = new FileOutputStream(file);
String s1 = "helloworld";
os.write(s1.getBytes());
os.close();
}
private static OutputStream FileOutputStream(File file) {
return null;
}
}
输入流+buff
package com.iteasyup.javase;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class TestBufferedInputStream {
public static void main(String[] args) throws IOException {
File file = new File("D:\\bb.txt");
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
int a = 0;
while ((a = bis.read()) != -1) {
System.out.println((char)a);
}
bis.close();
is.close();
}
}
输入流的使用
package com.iteasyup.javase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class TestInputstream {
public static void main(String[] args) throws IOException {
File file = new File("D:\\aa.txt");
InputStream is = new FileInputStream(file);
int result = 0;
while(((result) = is.read()) != -1) {
System.out.println((char)result);
}
is.close();
}
}
数据类型以及如何进行字符串之间的比较
package com.iteasyup.javase;
public class Test2 {
public static void main(String[] args) {
int a = 10;
int b = 20;
int c = a / b;
System.out.println(c);
double d = 1.5;
double e = 2;
System.out.println(d / e);
System.out.println(a != b);
boolean b1 = true;
boolean b2 = false;
String s1 = "java";
String s2 = "1001";
System.out.println(s1 + a);
System.out.println(!s1.equals(s2));
System.out.println(s1.equals(s2));
String v = "13599123456";
}
}
数据类型转换,装箱,拆箱,强转
package com.iteasyup.javase;
public class TestCollection {
public static void main(String[] args) {
Integer i = 100;
Character c = 'a';
int a= 10;
String s1 = String.valueOf(a);
System.out.println(s1);
String s2 = "123";
Integer i1 = Integer.valueOf(s2);
int a1 = i1;
int a2 = 100;
Integer i2 = a2;
int a3 = Integer.parseInt(s2);
}
}
数组
package com.iteasyup.javase;
public class 数组 {
public static void main(String[] args) {
int[] arr = {2,45,1,5};
arr[1] = 3;
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
条件分支语句
package com.iteasyup.javase;
import java.util.Scanner;
public class 条件分支语句 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("请输入学生成绩:");
double score = in.nextDouble();
if (score >= 90 && score <= 100) {
System.out.println("优秀");
}else if (score >= 80 && score < 90) {
System.out.println("良好");
}else if (score >= 60 && score < 80) {
System.out.println("及格");
}else if (score >= 0 && score <60) {
System.out.println("不及格");
}else {
System.out.println("成绩必须在0-100之间");
}
}
}
通过流和反射来创建对象并调用一些私有化的变量和方法
package com.iteasyup.javase;
public class Student {
int no;
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private void show(String s1) {
System.out.println(s1);
}
}
package com.iteasyup.javase;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestStudent {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
Student student = class1.newInstance();
Field[] declaredFields = class1.getDeclaredFields();
for (Field field : declaredFields) {
System.out.println(field);
}
Field[] fields = class1.getFields();
for (Field field2 : fields) {
System.out.println(field2);
}
Method[] methods = class1.getMethods();
for (Method method : methods) {
System.out.println(method);
}
Method method = class1.getMethod("setName", String.class);
method.invoke(student, "小红");
Method method2 = class1.getMethod("getName");
System.out.println(method2.invoke(student));
Method method3 = class1.getDeclaredMethod("show", String.class);
method3.setAccessible(true);
method3.invoke(student, "helloworld");
}
}
同一个class中进行方法调用
package com.iteasyup.javase;
public class TestMethod {
public static void main(String[] args) {
TestMethod.f1();
}
public static void f1() {
for (int i = 1; i < 11; i++) {
System.out.println(i);
}
}
public static void max(int c,int d) {
int a = c;
int b = d;
System.out.println(a > b ? a : b);
}
}
文件基本操作
package com.iteasyup.javase;
import java.io.File;
public class TestFile {
public static void main(String[] args) {
File file = new File("D:/VS Code/a.txt");
System.out.println(file.getName());
System.out.println(file.length());
System.out.println(file.exists());
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
File file2 = new File("D:\\bb.txt");
System.out.println(file2.getName());
System.out.println(file.length());
}
}
线程的两种创建方式
package com.iteasyup.javase;
public class Customer1 implements Runnable {
@Override
public void run() {
int money = 1000;
for (int i = 0; i < 10; i++) {
money -= 100;
System.out.println("当前账户余额:" + money);
}
}
}
package com.iteasyup.javase;
public class Customer2 extends Thread {
@Override
public void run() {
int money = 1000;
for (int i = 0; i < 10; i++) {
money += 100;
System.out.println("当前账户余额:" + money);
}
}
}
package com.iteasyup.javase;
public class TestCustomer {
public static void main(String[] args) {
Runnable c1 = new Customer1();
Thread c2 = new Customer2();
Thread cc = new Thread(c1);
cc.start();
c2.start();
}
}
有参构造方法的使用
package com.iteasyup.javase;
public class Animal {
String name;
int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void showAnimal() {
System.out.println("动物的名字叫做" + name + "年龄为" + age + "岁");
}
public static void main(String[] args) {
Animal animal = new Animal("小红", 10);
animal.showAnimal();
}
}
有参构造方法和无参构造方法的使用的一个例子
package com.lingyige.javase;
public class Film {
String filmName;
String actorName;
int money;
public Film() {
filmName = "这个杀手不太冷";
actorName = "让·雷诺";
money = 0;
}
public Film(String filmName, String actorName, int money) {
this.filmName = filmName;
this.actorName = actorName;
this.money = money;
}
public void addMoney(int day) {
money += 300000 * day;
}
public void showFilm() {
System.out.println(filmName + "这个电影很不错,主演是:" + actorName + ",它的票房总额是:" + money);
}
}
package com.lingyige.javase;
public class FilmTest {
public static void main(String[] args) {
Film f1 = new Film();
f1.addMoney(9);
f1.showFilm();
Film f2 = new Film("这个手刹不太灵", "小强", 0);
f2.addMoney(8);
f2.showFilm();
}
}
有参数的方法调用
package com.iteasyup.javase;
public class TestParameter {
public static void selectMoney(int money) {
money -= 10;
System.out.println("剩余" + money + "元");
}
public static void main(String[] args) {
int a = 100;
selectMoney(a);
}
}
注意不要将有参无参构造方法和方法混淆,两者的使用并不冲突
package com.lingyige.javase;
public class People {
String name;
int age;
String sex;
double height;
public People() {
name = "小刚";
age = 24;
sex = "男";
height = 160.0;
}
public void talk(String t) {
System.out.println(t + "说了一句受死吧");
}
public void changeName(String n) {
name = n;
}
public void addHeight(double n) {
height += n;
}
public void showPeople() {
if (height >= 180) {
System.out.println("他,身高" + height + "有余,孔武有力,年仅" + age + "岁,就以傲人的身高俯瞰众生,他就是" + name);
}
else {
System.out.println("他,虽身高只有" + "height" + ",却有着惊人的速度和灵活性,年仅" + age + "岁,就能快速游走于公交人群中,他就是" + name);
}
}
}
package com.lingyige.javase;
public class PeopleTest {
public static void main(String[] args) {
People people = new People();
people.talk("大吉哥");
people.changeName("vip");
people.addHeight(180.0);
people.showPeople();
}
}