Java基础知识(数据类型,输入,向量)

本文通过一个Java程序示例介绍了基本的数据类型使用、数组排序、标准输入输出及简单的数据结构如向量和哈希表的应用。同时,还展示了如何进行用户交互以及一些基本的算法实现。

 

package com.java;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import javax.swing.JOptionPane;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// 整数运算采用32位精度,长整数采用64位精度
		byte a, b = 1, c = 2;
		a = (byte) (b + c);

		// 将一个Unicode字符赋给一个变量
		char ch = '\u12ab';
		// 八进制转义序列
		ch = '\123';
		System.out.println(ch);

		// 八进制0123,十六进制0x123a
		int d = 0123;
		System.out.println(d);
		int e = 0x123a;
		System.out.println(e);

		long d1 = 123L;

		// 浮点数直接量默认为64位数值
		double d2 = 123.2D;
		double d22 = 123.2;
		float d3 = 123.2F;

		int[] a1 = { 1, 500, 100, 60, 20, 30 };
		Arrays.sort(a1);
		for (int i = 0; i < a1.length; i++) {
			System.out.print(a1[i] + ",");
		}
		System.out.println("");

		System.out.println(med3(a1, 1, 2, 3));

		// 输入框输入
		String s = JOptionPane.showInputDialog("Test");
		System.out.println(s);
		// 标准输入
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		try {
			s = bf.readLine();
			System.out.println(s);
			// 必须关闭,否则一直等待
			bf.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		// 向量
		Vector<Integer> v1 = new Vector<Integer>();
		v1.add(1);
		// 向量的当前元素大小1
		System.out.println(v1.size());
		// 向量的当前容量 10
		System.out.println(v1.capacity());

		// 遍历问题
		Map<String, String> map1 = new HashMap<String, String>();
		map1.put("1", "adfsdf");
		map1.put("2", "test");
		Set<String> keys = map1.keySet();
		for (String key : keys) {
			if (key.equals("1")) {
				keys.remove(key);
			}
		}
		System.out.println(map1);

	}

	// 返回key值最大的索引
	private static int med3(int x[], int a, int b, int c) {
		return (x[a] < x[b] ? (x[b] < x[c] ? b : x[a] < x[c] ? c : a)
				: (x[b] > x[c] ? b : x[a] > x[c] ? c : a));
	}

}
 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值