Java的一些類的使用經驗__Java

來源:互聯網
上載者:User

1.對字串的末尾的進行限定的方法(例:讓末尾不含,—,,)
while(strTrue.endsWith("+")||strTrue.endsWith("-")||strTrue.endsWith(","))//過濾掉末尾的++號
          strTrue=strTrue.substring(0,strTrue.length()-1);
2.一定要記住:對於數字要用==來比較,對於字串則要用.equals(String)來比較,否則對於==的比較始終為否。
3.String 類型安標準來說沒有長度限制,但是一般jdk中String的最大長度是4G
5.對於在java類中,在靜態法方法中,不能使用類的屬性變數。
6.對於Iterator 介面
  Collection 介面的iterator()方法返回一個 Iterator。Iterator介面方法能以迭代方式逐個訪問集合中各個元素,並安全的從Collection 中除去適當的元素。
  (1) boolean hasNext(): 判斷是否存在另一個可訪問的元素
      Object next(): 返回要訪問的下一個元素。如果到達集合結尾,則拋出NoSuchElementException異常。
  (2) void remove(): 刪除上次訪問返回的對象。本方法必須緊跟在一個元素的訪問後執行。如果上次訪問後集合已被修改,方法將拋出IllegalStateException。Iterator中刪除操作對底層Collection也有影響。
即在一個hasNext()下,不要多次的調用.next()方法,否則會出現:NoSuchElementException異常。
7.對於把字串轉成Integer類型時,對於一般的不要用Integer.getInteger("23"),它可能轉成一個null,因此是先把它轉成用Integer.ParseInt轉成int,然後強制類型轉換:new Integer(23)即。                                                      8.顯示一個yyyy-mm-dd hh:mm的時間
import java.util.*;
public class test{
  public static void main(String srt[])
  {
  Date d=new Date();
  GregorianCalendar  z=new GregorianCalendar();
  z.setTime(d);
  String dateTime=z.get(Calendar.YEAR)+"-"+z.get(Calendar.MONTH)+"-"+z.get(Calendar.DAY_OF_MONTH)+" "+z.get(Calendar.HOUR)+":"+z.get(Calendar.MINUTE);
   System.out.println(dateTime);
  System.out.println(d.toString());
  }
  }                                                                                                                                                                                             9.對於double和float型的書取得正負好的辦法:
math.sinnum(..);

下面有來自類庫的資訊
Class Math
java.lang.Object
  java.lang.Math
其中有有關加號或減號方法如下可以解決你的問題
static double signum(double d)
          Returns the signum function of the argument; zero if the argument is zero, 1.0 if the argument is greater than zero, -1.0 if the argument is less than zero.
static float signum(float f)
          Returns the signum function of the argument; zero if the argument is zero, 1.0f if the argument is greater than zero, -1.0f if the argument is less than zero.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.