输入逗号(特定符号间隔的数字)
以字符串的形式输入,通过split(‘punctuation’)分割,通过字符串数组保存分割到的 == 数字字符串==,利用Integer.parseInt()并将结果存入数组中。
Scanner scan = new Scanner (System.in);
String in = scan.nextLine();
String numS[]=in.split(",");
int[] num = new int [numS.length];
for(int i = 0 ; i < numS.length;i++)
{
num[i]=Integer.parseInt(numS[i]);
}
for(int i=0;i<numS.length;i++)
{
System.out.println(num[i]);
}
System.out.print(num); ///hahah
literals (字面上,文本)
You may have noticed that the new keyword isn’t used when initializing a variable of a primitive type. Primitive types are special data types built into the language; they are not objects created from a class. A literal is the source code representation of a fixed value; literals are represented directly in your code without requiring computation. As shown below, it’s possible to assign a literal to a variable of a primitive type:
boolean result = true;
char capitalC = 'C';
byte b = 100;
short s = 10000;
int i = 100000;
long 型数据需要在后面加上l或L 默认使int
float 后面加上F或f 默认使double
long
long类型的整数常数结尾一定为l或L(不区分大小写)。
♂DOM解析XML文件♂
转载:https://blog.youkuaiyun.com/a1353206432/article/details/80583302
public class Exercise{
public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc = builder.parse("src\\HelloXML.xml");
NodeList node = doc.getElementsByTagName("linkman");
for(int i=0;i<node.getLength();i++)
{
Element e = (Element)node.item(i);
System.out.println("name "+e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
System.out.println("email "+e.getElementsByTagName("mail").item(0).getFirstChild().getNodeValue());
}
}
}
JDBC JDK8 MySQL8.0.18 Connection/J 8.0.18
使用Connection/J中的驱动的时候需要修改Class.forName()中的参数为“com.mysql.cj.jdbc.Driver”
这里一定要注意Driver中的D需要大写!
url需要显示端指定关闭ssl即useSSL=false;
同时需要配置时区即&serverTimezone=地点
转载:https://blog.youkuaiyun.com/kikock/article/details/80413475
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.cj.xdevapi.Statement;
public class BataBase {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// TODO Auto-generated method stub
//System.out.println("ShuZhiYuan");
/*
* JDBC 四大配置参数
* >driverClassName: com.mysql.jdbc.Driver
* >url:jdbc:mysql://localhost:3306/数据库名
* >username:root
* >password:123456
*/
String url="jdbc:mysql://localhost:3306/mydb?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true";
// ? 起连接作用,通过?来带参数,连接域名何参数
//& 不同参数的间隔符
String username="root";
String password="123456";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
}catch(Exception e) {
System.out.println("加载驱动类失败");
}
//DriverManager class manage the establishment of connections
//use Class.forName() to implements the java.sql.Driver interface .If with connector/J ,use the com.mysql.cj.jdbc.Driver.
//obtain a Connection instance to connect particular database By DriverManager.getConnection()
//To create a Statement instance ,use the createStatement()method on the Connection Object.
//using JDBC Statement Object to Execute SQL
//retrieve the results through the ResultSet Class
Connection con = DriverManager.getConnection(url,username,password);
ResultSet rs ;
java.sql.Statement sql;
sql=con.createStatement();
rs=sql.executeQuery("Select * from first_table");
while(rs.next()) {
String name=rs.getString(1);
String age=rs.getString(2);
String gender=rs.getString(3);
System.out.println(name+"|"+age+"|"+gender);
}
}
}
** Binary Tree**
广义搜索二叉树–Level Order Traversal
从根节点开始,将其左右叶子节点放入链表中,读取链表中的第一个元素,取左右叶子结点放入链表中,输出链表中第一个元素的值。直至链表为空。
static void levelOrder(Node root){ //广义搜索二叉树
//Write your code here
Node popList;
LinkedList<Node> myLinkedList=new LinkedList<Node>();
myLinkedList.add(root);
while(myLinkedList.size()!=0) {
popList=myLinkedList.pop(); //读取列表中的第一个元素
if(popList.left!=null) //添加左子树的根节点
myLinkedList.add(popList.left);
if(popList.right!=null) //添加右子树的根节点
myLinkedList.add(popList.right);
System.out.print(popList.data+" "); //输出列表中第一个元素的数据
}
}
Math
很有用的类
链表,队列,栈
Java中对栈的操作已经有了现成的接口和实现类,通过已经造好的轮子即可实现操作——仅声明一个对象再通过对象的方法即可实现。
对于栈的操作官方文档如是解释:https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html
Deque<Character> stack=new ArrayDeque<Character>(); //初始栈的对象
void pushCharacter(char c) {
stack.push(c); //入栈
}
char popCharacter() {
return stack.pop(); //出栈——读取第一个元素并删除
}
对于队列的操作https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html
队列提供了一个Queue接口,同时有许多的类用于实现这个接口这里用到了LinkedList类根据接口的特点,实现类实现了接口中的所有方法因此,可以通过接口中的几个方法实现对队列的操作。
void enqueueCharacter(char c) {
myLinkedList.add(c); //入队列,每一个元素加到队尾
}
char dequeueCharacter() {
return myLinkedList.poll(); //出队列,将队首的队列输出并删除
}
Java中 Java.util.LinkedList 类可以灵活的操纵链表
通过LinkList创建链表对象,通过操作这个myLinkList对象实现对链表的操作。
官方文档:https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#addAll(int,%20java.util.Collection)
import static org.junit.jupiter.api.Assertions.*;
import java.util.LinkedList;
import org.junit.jupiter.api.Test;
class SolutionTest {
@Test
void test() {
LinkedList<Integer> myLinkedList= new LinkedList<Integer>();
System.out.println(myLinkedList.size());//输出链表长度初始长度为1
myLinkedList.add(1); //在尾部加一个元素
myLinkedList.add(2);
myLinkedList.addFirst(3);
myLinkedList.addLast(4);
myLinkedList.add(2,5); //根据索引加一个元素
System.out.println(myLinkedList.pop());//使第一个元素出栈
System.out.println(myLinkedList);
System.out.println(myLinkedList.get(1));//根据索引输出一个元素
myLinkedList.clear(); //清空链表
System.out.println(myLinkedList);
}
}
Annotation——注解
JDK5.0引入的一种注释机制,可以帮助理解,编写代码
@Override - 检查该方法是否是重载方法。如果发现其父类,或者是引用的接口中并没有该方法时,会报编译错误。
@Deprecated - 标记过时方法。如果使用该方法,会报编译警告。
@SuppressWarnings - 指示编译器去忽略注解中声明的警告。
作用在其他注解的注解(或者说 元注解)是:
@Retention - 标识这个注解怎么保存,是只在代码中,还是编入class文件中,或者是在运行时可以通过反射访问。
@Documented - 标记这些注解是否包含在用户文档中。
@Target - 标记这个注解应该是哪种 Java 成员。
@Inherited - 标记这个注解是继承于哪个注解类(默认 注解并没有继承于任何子类)
例如重写父类方法时打上@Override标签可以帮助检查拼写错误(重写的method名字必须与父类中method中的名字相同)。
继承
用子类创建对象,先调用父类的构造方法,在执行子类的构造方法。
在继承关系的前提下,子类一定会调用弗雷德 构造方法。因为构造方法是用来进行初始化的,子类要初始化,必须先使用父类的构造方法。
[转载]https://blog.youkuaiyun.com/DTEzhihao/article/details/86765119
//子类构造方法一定调用父类的构造方法默认调用无参的构造方法,
//若父类重写了构造方法(一般不存在无参构造方法)则需要用super()
//调用所需的构造方法,构造父类。
关于子类与父类构造方法
import java.util.*;
class Day12{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String firstName = scan.next();
String lastName = scan.next();
int id = scan.nextInt();
int numScores = scan.nextInt();
int[] testScores = new int[numScores];
for(int i = 0; i < numScores; i++){
testScores[i] = scan.nextInt();
}
scan.close();
Student s = new Student(firstName, lastName, id, testScores);
s.printPerson();
System.out.println("Grade: " + s.calculate() );
}
}
class Person{
protected String firstName;
protected String lastName;
protected int idNumber;
// Constructor
Person(String firstName, String lastName, int identification){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = identification;
}
// Print person data
public void printPerson(){
System.out.println(
"Name: " + lastName + ", " + firstName
+ "\nID: " + idNumber);
}
}
class Student extends Person{
String firstName;
String lastName;
int id;
int [] scores;
Student(String firstName,String lastName,int id,int []scores){
super(firstName,lastName,id); //子类构造方法一定调用父类的构造方法默认调用无参的构造方法,
this.firstName=firstName; //若父类重写了构造方法(一般不存在无参构造方法)则需要用super()
this.lastName=lastName; //调用所需的构造方法,构造父类。
this.id=id;
this.scores=scores;
}
char calculate()
{
char character = 0;
int sum=0;
for(int i=0;i<scores.length;i++)
{
sum+=scores[i];
}
sum=sum/scores.length;
if(sum>=90)
character='O';
else
if(sum>=80)
character='E';
else
if(sum>=70)
character='A';
else
if(sum>=55)
character='P';
else
if(sum>=40)
character='D';
else
character='T';
return character;
}
}
泛型
[转载]
1、泛形要求能包容的是对象类型,而基本类型在java里不属于对象。
但是基本类型都有其包装类型,也就是对象类型: int->Integer long->Long …
2、java, 泛型问题,就是<>里面可以填写哪些类型?
想放int的话要写integer,不能光一个int的,int 是基本数据类型,Integer是其包装类,注意是一个类,泛型也是要写个类的,所以要用integer。
创建泛型类对象需要传递数据类型
@test
class test{
Generic <Integer> a=new Gereric<T>();
}
class Generic <T>{
}
import static org.junit.jupiter.api.Assertions.*;
import java.util.*;
import org.junit.jupiter.api.Test;
class Generic2 {
@Test
void test() {
Generic_tools <Integer> one = new Generic_tools<Integer>();
System.out.println("The number of the integer array member");
Scanner scan_integer = new Scanner(System.in);
int n= scan_integer.nextInt();
Integer[] a=new Integer[n];
inputInteger(a);
one.max(a);
one.min(a);
one.avg(a);
Generic_tools <Double> two = new Generic_tools<Double>();
System.out.println("The number of the double array member");
Scanner scan_double = new Scanner(System.in);
int m= scan_double.nextInt();
Double[] b=new Double[m];
inputDouble(b);
two.max(b);
two.min(b);
two.avg(b);
}
void inputInteger(Integer a[])
{
Scanner scan=new Scanner(System.in);
for(int i=0;i<a.length;i++)
{
a[i]=scan.nextInt();
}
}
void inputDouble(Double a[])
{
Scanner scan=new Scanner(System.in);
for(int i=0;i<a.length;i++)
{
a[i]=(Double)scan.nextDouble();
}
}
}
class Generic_tools <T extends Number> {
/* T a[];
void input (T a[])
{
this.a=a;//成员数组存储传来的数组引用
}
*/
void max(T a[]) {
T max_num;
max_num=a[0];
for(T num:a)
{
if(a[0] instanceof Integer)
{
if(((Integer)max_num).intValue()<((Integer)num).intValue())
max_num=num;
}
if(a[0] instanceof Double)
{
if(((Double)max_num).intValue()<((Double)num).intValue())
max_num=num;
}
}
System.out.println("Output the max value : "+max_num);
}
void min(T a[]) {
T min_num;
min_num=a[0];
for(T num:a)
{
if(a[0] instanceof Integer)
{
if(((Integer)min_num).intValue()>((Integer)num).intValue())
min_num=num;
}
if(a[0] instanceof Double)
{
if(((Double)min_num).intValue()>((Double)num).intValue())
min_num=num;
}
}
System.out.println("Output the min value : "+min_num);
}
void avg(T a[]) {
if(a[0] instanceof Integer)
{
int sum=0;
for(T num:a)
{
sum+=((Integer)num).intValue();
}
System.out.println("Output the Average value : "+sum/a.length);
}
if(a[0] instanceof Double)
{
double sum=0;
for(T num:a)
{
sum+=((Double)num).doubleValue();
}
System.out.println("Output the Average value : "+sum/(double)a.length);
}
}
}
数组
数组的声明
数组元素类型 数组名[];
数组元素类型 [] 数组名;
数组元素类型可以是基本数据类型也可以是类类型,
Java不允许在声明数组的时候指定数组元素个数。
为数组分配元素
数组名 = new 数组元素的类型[个数];
二维数组的一维数组不必有相同长度:
数组类型 数组名[][]=new 数组类型[n][];
数组名[0]=new 数组类型[x];
数组名[1]=new 数组类型[z];
创建数组后系统会给每个元素一个默认值。
二维数组名.length得到一位数组元素个数
Java Dictionaries and Maps
通过MAP接口创建对象在同过HashMap实现,完成Key-Value存储何读取
通过调用containsKey(Object key)方法判断是否存在Key;
通过get(Object key)获得value。
通过put(key,value)进行数据存储。
import java.util.*;
class Day6_Lets_Review{
public static void main(String args[]) {
String st;
Map<String,String> myMap = new HashMap<String,String>();
Scanner scan=new Scanner (System.in);
int n=scan.nextInt(); //define the number of input
for(int i=0;i<n;i++)
{
myMap.put(scan.next(), scan.next()); //.next() ignore the blank and the newline character
}
while(scan.hasNext())
{
st=scan.next();
if(myMap.containsKey(st)) //judge the key whether exist
System.out.println(st+"="+myMap.get(st)); //according the key return the value
else
System.out.println("Not Found");
}
}
}
2019.10.9 凉爽
今天看到了同校ACM实验室同学写的Blog发现自己所在的团队真的有些差劲,体制的问题,……没什么可说的,战斗青岛队。
Java实例方法和类方法的区别
在Java练习的过程中,在main方法中调用了一个在本类中实现的方法提示错误,根据错误提示其在所调用的前加以static进行修饰,不得其解,遂百度,得知类方法不能操纵实例方法,于是明白。后查书,书中述,类方法在该类被加载到内存中,即分配了相应的入口地址,而实例方法只有在创建对象后才能分配入口。所以类方法不可以操纵实例方法。同理类变量和实例变量如此。
String类
将字符串转换成字符数组的方法char []myStringArray=string.toCharArray();
public class Day6_Lets_Review {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String s;
int n = scan.nextInt();
for (int o=0;o<n;o++)
{
s=scan.next();
char[]myStringArry=s.toCharArray();
for(int i=0;i<myStringArry.length;i+=2)
{
System.out.print(myStringArry[i]);
}
System .out.print(" ");
for(int j=1;j<myStringArry.length;j+=2)
System.out.print(myStringArry[j]);
System.out.println();
}
}
}