java

java(一)

public class hello{
	public static void main(String[] args){
		System.out.println("hello world");
	}
}

编译:javac hello.java

运行:java hello

public class Hello{
	public static void main(String[] args){
		String str="今天是二号";
		System.out.println(str);
		System.out.println(str);
		System.out.println(str);
		System.out.println(str);
		System.out.println(str);
		System.out.println(str);

	}
}

Java的8个基本数据类型

字符:

  • char

整数:

  • byte单字节数:占1个字节,8位,-128~127
  • short双字节数:占2个字节,16位,-32768~32767
  • int四字节数:占4个字节,32位,-2147483648~2147483647
  • long:八字节数,64位

浮点数:

  • float:单精度浮点数,占用4个字节
  • double:双精度浮点数,占用8个字节

布尔:

  • boolean:只有两个值,true和false

整数默认是int类型

小数默认是double类型

public class Hello{
	public static void main(String[] args){
		char c='a';
		System.out.println(c);
		byte bt=8;
		System.out.println(bt);
		short sh=88;
		System.out.println(sh);
		int in=888;
		System.out.println(in);
		long lo=8888;
		System.out.println(lo);
		double db=6.28;
		System.out.println(db);
		float fl=3.14f;
		System.out.println(fl);
		boolean bl=true;
		System.out.println(bl);

	}
}

Scanner的应用:

import java.util.Scanner;

public class Hello{
	public static  void main(String[] args){
		//创建Scanner对象
		Scanner sca=new Scanner(System.in);
		//从命令行获取用户输入的数据
		System.out.println("请输入字符串:");
		String str=sca.next();
		System.out.println(str);
		
		System.out.println("请输入数字:");
		int num=sca.nextInt();
		System.out.println(num);
		
		System.out.println("请输入小数:");
		double dou=sca.nextDouble();
		System.out.println(dou);
	}
}

循环

public class Hello{
	public static  void main(String[] args){
		int i=0;
		while(i<10){
			System.out.println("while循环");
			i++;
		}
		for(int j=0;j<10;j++){
			System.out.println("for循环");
		}
	}
}

方法:

变量名不能使用java的关键字

public class Hello{
	public static  void main(String[] args){
		int a=10;
		int b=18;
		int result=maxa(a,b);
		System.out.println("最大的数为"+ result);
	}
	public static int maxa(int a,int b){
		if(a>b){
			return a;
		}
		else{
			return b;
		}
	}
}

数组:

package hello;
public class HelloWorld {
	public static void main(String[] args) {
		float[] a=new float[3];
		a[0]=1;
		a[1]=2;
		a[2]=3;
		for(int i=0;i<a.length;i++) {
			System.out.println(a[i]);
		}
		double[] b=new double[] {4,5,6};
		for(int i=0;i<b.length;i++) {
			System.out.println(b[i]);
		}
		double[] c= {7,8,9};
		for(int i=0;i<c.length;i++) {
			System.out.println(c[i]);
		}
	}
}

字符串

package hello;
public class HelloWorld {
	public static void main(String[] args) {
		String str="1号,2号,3号,4号,5号";
		//用逗号分隔字符串
		String[] a=str.split(",");
		for(int i=0;i<a.length;i++) {
			System.out.println(a[i]);
		}
	}
}

带括号的是方法,不带括号的是属性

package hello;
public class HelloWorld {
	public static void main(String[] args) {
		String str="好家伙123";
		//截取字符串,参数为开始截取的下标
		String test=str.substring(3);
		System.out.println(test);
		//两个参数,第一个参数为开始截取的下标,第二个参数为终止截取的下一个字符的下标
		String test1=str.substring(1,4);
		System.out.println(test1);
	}
}

字符串判断相等要用equals方法,用==有时候不准确

package hello;
public class HelloWorld {
	public static void main(String[] args) {
		String a="123";
		String b=new String("123");
		if(a==b) {
			System.out.println("两个字符串相等");
		}
		else {
			System.out.println("两个字符串不相等");
		}
		
		if(a.equals(b)) {
			System.out.println("两个字符串相等");
		}
		else {
			System.out.println("两个字符串不相等");
		}
	}
}

ArrayList

package hello;

import java.util.ArrayList;

public class HelloWorld {
	public static void main(String[] args) {
		ArrayList<String> number = new ArrayList<String>();
		number.add("一");
		number.add("二");
		number.add("三");
		number.add("四");
		number.add("五");
		
		for(int i=0;i<number.size();i++) {
			System.out.println(number.get(i));
		}

	}
}

package hello;

import java.util.ArrayList;

public class HelloWorld {
	public static void main(String[] args) {
		ArrayList<String> number = new ArrayList<String>();
		number.add("一");
		number.add("二");
		number.add("三");
		number.add("四");
		number.add("五");
		
		for(int i=0;i<number.size();i++) {
			System.out.println(number.get(i));
		}
		System.out.println("--------------------");
		//删除指定下标的元素
		number.remove(3);
		for(int i=0;i<number.size();i++) {
			System.out.println(number.get(i));
		}
		System.out.println("-----------------------");
		//删除指定对象的元素
		number.remove("二");
		for(int i=0;i<number.size();i++) {
			System.out.println(number.get(i));
		}
		System.out.println("------------------------");
		//替换索引下标的元素
		number.set(0, "六");
		for(int i=0;i<number.size();i++) {
			System.out.println(number.get(i));
		}
		System.out.println("----------------------");
		//在索引下标的元素前加入元素
		number.add(2,"七");
		for(int i=0;i<number.size();i++) {
			System.out.println(number.get(i));
		}
	}
}

HashSet

package hello;

import java.util.HashSet;
import java.util.Iterator;

public class HelloWorld {
	public static void main(String[] args) {
		HashSet<String> set=new HashSet<String>();
		set.add("雷泽");
		set.add("凯亚");
		set.add("温迪");
		set.add("雷泽");
		
		Iterator<String> itr=set.iterator();
		
		while(itr.hasNext()) {
			String tmp=itr.next();
			System.out.println(tmp);
		}
	}
}

HashMap

package hello;

import java.util.Map.Entry;
import java.util.Set;
import java.util.HashMap;

public class HelloWorld {
	public static void main(String[] args) {
		HashMap<String,String> map=new HashMap<String,String>();
		map.put("雷泽", "四星");
		map.put("凯亚","四星");
		map.put("温迪", "五星");
		map.put("飞天御剑", "三星");
		map.put("班尼特", "六星");
		
		System.out.println(map.get("雷泽"));
		
		System.out.println(map.containsKey("温迪"));
		System.out.println(map.containsKey("迪卢克"));
		
		Set<Entry<String,String>> set=map.entrySet();
		for(Entry<String,String> entry:set) {
			System.out.println(entry.getKey()+"--->"+entry.getValue());
		}
	}
}

文件操作

package hello;

import java.io.File;
import java.io.IOException;

public class HelloWorld {
	public static void main(String[] args) throws IOException {
		File file=new File("D:/game/a.txt");
		//判断文件是否存在
		System.out.println(file.exists());
		//判断是否是文件夹
		System.out.println(file.isDirectory());
		//获取文件名
		System.out.println(file.getName());
		//获取文件路径
		System.out.println(file.getPath());
		//删除文件
		file.delete();
		//创建文件
		file.createNewFile();
		
	}
}

package hello;

import java.io.File;
import java.io.IOException;

public class HelloWorld {
	public static void main(String[] args) throws IOException {
		File file2=new File("D:/game/aa");
		System.out.println(file2.isDirectory());
		//获取aa文件夹中的每一个子文件和子文件夹构造出来的file对象
		File[] files=file2.listFiles();
		for(File tmp:files) {
			System.out.println(tmp.getName());
		}
	}
}

package hello;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class HelloWorld {
	public static void main(String[] args) throws IOException {
		File file=new File("D:/game/a.txt");
		FileInputStream fis=new FileInputStream(file);
		//读取一个字节
		int i=fis.read();
		System.out.println(i);
		
//		while((i=fis.read())!=-1) {
//			System.out.print((char)i);
//		}
		byte[] b=new byte[1024];
		int len=0;
		while((len=fis.read(b))!=-1) {
			String text=new String(b);
			System.out.print(text);
		}
                fis.close();
	}
}

package hello;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class HelloWorld {
	public static void main(String[] args) throws IOException {
		File file=new File("D:/game/a.txt");
		FileOutputStream fos=new FileOutputStream(file);
		fos.write("hello world".getBytes());
		fos.close();
	}
}

从json文件中取出数据并分析,计算出每个movie被评论了多少次,

package hello;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class HelloWorld {
	public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("D:/game/jsonDemo.json");
		//按行读取文件
		BufferedReader br=new BufferedReader(new InputStreamReader(fis));
		
		HashMap<String,Integer> countMap=new HashMap<String,Integer>();
		
		String line=" ";
		while( (line=br.readLine())!=null  ) {
			//截系json串
			JSONObject jsonObject=JSON.parseObject(line);
			//抽取movieID
			String movieID=jsonObject.getString("movie");
			
			if(countMap.containsKey(movieID)) {
				int count=countMap.get(movieID);
				countMap.put(movieID, count+1);
			}else {
				countMap.put(movieID, 1);
			}
			
		}
		br.close();
		FileOutputStream fos=new FileOutputStream("D:/game/result.txt");
		Set<Entry<String,Integer>> entrySet=countMap.entrySet();
		for(Entry<String,Integer> ent:entrySet) {
			fos.write((ent.getKey()+"--->"+ent.getValue()+"
").getBytes());
		}
		fos.close();
		
	}
}

测试json文件

{"movie":"tiger"}
{"movie":"mountain"}
{"movie":"tiger"}
{"movie":"good"}
{"movie":"tiger"}
{"movie":"mountain"}

原文地址:https://www.cnblogs.com/fate-/p/14633364.html