zoukankan      html  css  js  c++  java
  • Java学习笔记(持续更新ing)

    1、在读入字符串时:    str = sc.nextLine();     //读入一行
                                        str = sc.next();   // 只是读入字符串

    2、在结构体的表示中用类来表示。

    //down的地址https://blog.csdn.net/fyp19980304/article/details/80448060
    方法一:把要存储的数据设为私有变量,然后另写函数对其进行读写,set()和get()
     
    public  class Test
    {
        private int x;
        private int y;
        public int getX() {
            return x;
        }
        public void setX(int x) {
            this.x = x;
        }
        public int getY() {
            return y;
        }
        public void setY(int y) {
            this.y = y;
        }
    }
    
    方法二: 把要存储的数据设为public变量,在其他主函数类中直接访问修改。
     
    class Supply implements Comparable<Supply>{
    	int number;
    	int max;
    	int spend;
    }
    

    3、Java中保留小数点后2位的方法(转)
     

    import java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    
    public class Test {
    	public static void main(String[] args) {
    		double d = 756.2345566;
    
    //方法一:最简便的方法,调用DecimalFormat类
    		DecimalFormat df = new DecimalFormat(".00");
    		System.out.println(df.format(d));
    
    //方法二:直接通过String类的format函数实现
    		System.out.println(String.format("%.2f", d));
    
    //方法三:通过BigDecimal类实现
    		BigDecimal bg = new BigDecimal(d);
    		double d3 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    		System.out.println(d3);
    
    //方法四:通过NumberFormat类实现
    		NumberFormat nf = NumberFormat.getNumberInstance();
    		nf.setMaximumFractionDigits(2);
    		System.out.println(nf.format(d));
    
    	}
    }
    

    4、Split

    在java.lang包中有String.split()方法,返回是一个数组

    1、如果用“.”作为分隔的话,必须是如下写法,String.split("\."),这样才能正确的分隔开,不能用String.split(".");

    2、如果用“|”作为分隔的话,必须是如下写法,String.split("\|"),这样才能正确的分隔开,不能用String.split("|");

    “.”和“|”都是转义字符,必须得加"\";

    3、如果在一个字符串中有多个分隔符,可以用“|”作为连字符,比如,“acount=? and uu =? or n=?”,把三个都分隔出来,可以用String.split("and|or");

    使用String.split方法分隔字符串时,分隔符如果用到一些特殊字符,可能会得不到我们预期的结果。 

    5、trim():去掉字符串首尾的空格。

    去掉字符串行首和行末的空格。

    s = s.trim();

    6、replace 字符串替换

     st = st.replace(s, t); 

    在Java中,在字符串 st 中要将一段子串 s 替换成另一段子串 t,这个时候可以用 replace。

    7、 indexOf  查询

    《indexOf用法转自https://www.cnblogs.com/xiaoke111/p/7660707.html》

    indexOf 有四种用法:

    1.indexOf(int ch) 在给定字符串中查找字符(ASCII),找到返回字符数组所对应的下标找不到返回-1

    2.indexOf(String str) 在给定符串中查找另一个字符串。。。

    3.indexOf(int ch,int fromIndex)从指定的下标开始查找某个字符,查找到返回下标,查找不到返回-1

    4.indexOf(String str,int fromIndex)从指定的下标开始查找某个字符串。。。

  • 相关阅读:
    为or、in平反——or、in到底能不能利用索引?
    【自然框架】稳定版beta1的Demo —— 四:角色管理。另外 在线演示 终于搞定了
    【自然框架】CMS之数据库设计
    【自然框架】——重开在线演示
    刘谦魔术的一个秘密 硬币穿越玻璃的那个。
    【自然框架】PowerDesigner 格式的元数据的表结构
    【自然框架】元数据的数据库结构的详细说明和示例(一):项目描述部分
    程序员与项目经理
    Android 中文API (70) —— BluetoothDevice[蓝牙]
    Android API 中文 (50) —— SpinnerAdapter
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139514.html
Copyright © 2011-2022 走看看