dsa代碼java dsl代碼

java如何實(shí)現(xiàn)按照尾字母排序(從右側(cè)往左側(cè)排)

我的實(shí)現(xiàn),做了兩次String的翻轉(zhuǎn),然后進(jìn)行比較~

成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、西鄉(xiāng)塘網(wǎng)絡(luò)推廣、成都微信小程序、西鄉(xiāng)塘網(wǎng)絡(luò)營(yíng)銷、西鄉(xiāng)塘企業(yè)策劃、西鄉(xiāng)塘品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);成都創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供西鄉(xiāng)塘建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn

import java.util.Arrays;

public class MySort {

public static void main(String[] args) {

String[] ary = { "as", "sa", "ads", "dsa" };

for (String tmp : ary) {

tmp = new StringBuilder(tmp).reverse().toString();//做字符串的翻轉(zhuǎn)

}

Arrays.sort(ary);//進(jìn)行排序~

for (String tmp : ary) {

tmp = new StringBuilder(tmp).reverse().toString();//翻轉(zhuǎn)回字符串原來的樣子~

System.out.print(tmp + " ");

}

}

}

java中的斷言問題

下面介紹一下斷言在JAVA中的使用,JAVA是從JDK1.4才開始支持?jǐn)嘌缘模ㄌ砑恿岁P(guān)鍵字assert),請(qǐng)注意老版的JRE不支持。

斷言概述

編寫代碼時(shí),我們總是會(huì)做出一些假設(shè),斷言就是用于在代碼中捕捉這些假設(shè)

可以將斷言看作是異常處理的一種高級(jí)形式

斷言表示為一些布爾表達(dá)式,程序員相信在程序中的某個(gè)特定點(diǎn)該表達(dá)式值為真

可以在任何時(shí)候啟用和禁用斷言驗(yàn)證,因此可以在測(cè)試時(shí)啟用斷言而在部署時(shí)禁用斷言。同樣,程序投入運(yùn)行后,最終用戶在遇到問題時(shí)可以重新起用斷言。

使用斷言可以創(chuàng)建更穩(wěn)定,品質(zhì)更好且易于除錯(cuò)的代碼

當(dāng)需要在一個(gè)值為FALSE時(shí)中斷當(dāng)前操作的話,可以使用斷言

單元測(cè)試必須使用斷言(Junit/JunitX)

除了類型檢查和單元測(cè)試外,斷言還提供了一種確定個(gè)種特性是否在程序中得到維護(hù)的極好的方法

使用斷言使我們向按契約式設(shè)計(jì)更近了一部

常見的斷言特性

前置條件斷言:代碼執(zhí)行之前必須具備的特性

后置條件斷言:代碼執(zhí)行之后必須具備的特性

前后不變斷言:代碼執(zhí)行前后不能變化的特性

斷言使用方式

斷言可以有兩種形式

1.assert Expression1

2.assert Expression1:Expression2

其中Expression1應(yīng)該總是一個(gè)布爾值,Expression2是斷言失敗時(shí)輸出的失敗消息的字符串。如果Expression1為假,則拋出一個(gè) AssertionError,這是一個(gè)錯(cuò)誤,而不是一個(gè)異常,也就是說是一個(gè)不可控制異常(unchecked Exception),AssertionError由于是錯(cuò)誤,所以可以不捕獲,但不推薦這樣做,因?yàn)槟菢訒?huì)使你的系統(tǒng)進(jìn)入不穩(wěn)定狀態(tài)。

起用斷言

斷言在默認(rèn)情況下是關(guān)閉的,要在編譯時(shí)啟用斷言,需要使用source1.4標(biāo)記 既javac source1.4 Test.java ,在運(yùn)行時(shí)啟用斷言需要使用 -ea參數(shù) 。要在系統(tǒng)類中啟用和禁用斷言可以使用 -esa 和 -dsa參數(shù)。

例如:

public class AssertExampleOne{

public AssertExampleOne(){}

public static void main(String args[]){

int x=10;

System.out.println("Testing Assertion that x==100");

assert x=100;"Out assertion failed!";

System.out.println("Test passed!");

}

}

如果編譯時(shí)未加 -source1.4,則編譯通不過

在執(zhí)行時(shí)未加 -ea 時(shí)輸出為

Testing Assertion that x==100

Test passed

jre忽略了斷言的就代碼,而使用了該參數(shù)就會(huì)輸出為

Testing Assertion that x==100

Exception in thread "main" java.lang.AssertionError: Out assertion failed!

at AssertExampleOne.main(AssertExampleOne.java:6)

斷言的副作用

由于程序員的問題,斷言的使用可能會(huì)帶來副作用 ,例如:

boolean isEnable=false;

//...

assert isEnable=true;

這個(gè)斷言的副作用是因?yàn)樗薷牧顺绦蛑凶兞康闹挡⑶椅磼伋鲥e(cuò)誤,這樣的錯(cuò)誤如果不細(xì)心的檢查是很難發(fā)現(xiàn)的。但是同時(shí)我們可以根據(jù)以上的副作用得到一個(gè)有用的特性,根據(jù)它來測(cè)試斷言是否打開。

public class AssertExampleTwo{

public static void main(String args[]){

boolean isEnable=false;

//...

assert isEnable=true;

if(isEnable==false){

throw new RuntimeException("Assertion shoule be enable!");

}

}

}

何時(shí)需要使用斷言

1.可以在預(yù)計(jì)正常情況下程序不會(huì)到達(dá)的地方放置斷言 :assert false

2.斷言可以用于檢查傳遞給私有方法的參數(shù)。(對(duì)于公有方法,因?yàn)槭翘峁┙o外部的接口,所以必須在方法中有相應(yīng)的參數(shù)檢驗(yàn)才能保證代碼的健壯性)

3.使用斷言測(cè)試方法執(zhí)行的前置條件和后置條件

4.使用斷言檢查類的不變狀態(tài),確保任何情況下,某個(gè)變量的狀態(tài)必須滿足。(如age屬性應(yīng)大于0小于某個(gè)合適值)

什么地方不要使用斷言

斷言語句不是永遠(yuǎn)會(huì)執(zhí)行,可以屏蔽也可以啟用

因此:

1.不要使用斷言作為公共方法的參數(shù)檢查,公共方法的參數(shù)永遠(yuǎn)都要執(zhí)行

2.斷言語句不可以有任何邊界效應(yīng),不要使用斷言語句去修改變量和改變方法的返回值

下邊是介紹斷言的用法:

assert是在J2SE1.4中引入的新特性,assertion就是在代碼中包括的布爾型狀態(tài),程序員認(rèn)為這個(gè)狀態(tài)是true。一般來說assert在開發(fā)的時(shí)候是檢查程序的安全性的,在發(fā)布的時(shí)候通常都不使用assert。在1.4中添加了assert關(guān)鍵字和java.lang.AssertError類的支持。

首先,我們有必要從一個(gè)例子說起assert

public class AssertTest

{

public static void main(String[] args)

{

AssertTest at = new AssertTest();

at.assertMe(true);

at.assertMe(false);

}

private void assertMe(boolean boo)

{

assert boo?true:false;

System.out.println("true condition");

}

}

程序中包含了assert的話,你要用javac -source 1.4 xxx.java來編譯,否則編譯器會(huì)報(bào)錯(cuò)的。要想讓assert得部分運(yùn)行的話,要使用java -ea xxx來運(yùn)行,否則包含assert得行會(huì)被忽略。下面我們運(yùn)行

javac -source 1.4 AssertTest.java

java -ea AssertTest

看看結(jié)果的輸出是:

true condition

Exception in thread "main" java.lang.AssertionError

at AssertTest.assertMe(AssertTest.java:13)

at AssertTest.main(AssertTest.java:7)

當(dāng)我們運(yùn)行at.assertMe(true)得時(shí)候,由于assert boo?true:false相當(dāng)于 assert true;因此沒有任何問題,程序往下執(zhí)行打印出true condition,但是執(zhí)行at.assertMe(false)的時(shí)候相當(dāng)于assert false,這個(gè)時(shí)候解釋器就會(huì)拋出AssertionError了,程序就終止了。大家必須清楚AssertionError是繼承自Error得,因此你可以不再程序中catch它的,當(dāng)然你也可以在程序中catch它然后程序可以繼續(xù)執(zhí)行。例如:

public class AssertTest

{

public static void main(String[] args)

{

AssertTest at = new AssertTest();

try

{

at.assertMe(true);

at.assertMe(false);

}

catch(AssertionError ae)

{

System.out.println("AsseriontError catched");

}

System.out.println("go on");

}

private void assertMe(boolean boo)

{

assert boo?true:false;

System.out.println("true condition");

}

}

assert還有另外一種表達(dá)的方式,就是assert exp1:exp2;其中exp1是個(gè)boolean返回值得表達(dá)式,而exp2可以是原始的數(shù)據(jù)類型或者對(duì)象都可以例如:

boolean boo = true;

String str = null;

assert boo = false:str="error";

我們剛開始講得assert exp1得形式,當(dāng)exp1是false得時(shí)候,AssertionError得默認(rèn)構(gòu)造器會(huì)被調(diào)用,但是assert exp1:exp2這樣的形式,當(dāng)exp1為true的時(shí)候后面exp2被或略,如果false的話,后面的表達(dá)式的結(jié)果會(huì)被計(jì)算出來并作為AssertionError得構(gòu)造器參數(shù)??聪旅娴睦樱?/p>

public class AssertTest

{

public static void main(String[] args)

{

AssertTest at = new AssertTest();

at.assertMe(true);

at.assertMe(false);

}

private void assertMe(boolean boo)

{

String s = null;

assert boo?true:false:s = "hello world";

System.out.println("true condition");

}

}運(yùn)行的時(shí)候會(huì)得到這樣的結(jié)果

true condition

Exception in thread "main" java.lang.AssertionError: hello world

at AssertTest.assertMe(AssertTest.java:14)

at AssertTest.main(AssertTest.java:7)

Assert最好不要濫用,原因是assert并不一定都是enable的,下面兩種情況就不應(yīng)該用assert

不要再public的方法里面檢查參數(shù)是不是為null之類的操作

例如public int get(String s)

{

assert s != null;

}

如果需要檢查也最好通過if s = null 拋出NullPointerException來檢查

不要用assert來檢查方法操作的返回值來判斷方法操作的結(jié)果

例如 assert list.removeAll();這樣看起來好像沒有問題 但是想想如果assert 被disable呢,那樣他就不會(huì)被執(zhí)行了 所以removeAll()操作就沒有被執(zhí)行 可以這樣代替

boolean boo = list.removeAl();

assert boo;

就說這么多吧,assert是scjp1.4的考試內(nèi)容 所以還是有必要了解的

關(guān)于JAVA中this的應(yīng)用

this.a →private int a;

this.b →private int b;

因?yàn)轭惓蓡T變量有a,b,這個(gè)方法中傳有參數(shù)a,b。用this表示的是類成員變量a,不用this表示此方法傳過來的參數(shù)a

不用this也可以,但是參數(shù)名得變,如下:

interface A

{

void show();

}

interface B

{

void add(int a,int b);

}

class C implements A,B

{

private int a,b;

public void show()

{

System.out.println(a+b);

}

public void add(int c,int d)

{

a = c

b = d;

}

class D

{

public static void main(String[] args)

{

C c=new C();

c.add(4,2);

c.show();//通過該函數(shù)打印以上兩個(gè)數(shù)的和。

}

}

求Java代碼:將D盤a.txt文件中含有"AA","BB"的行留下,其余的都刪除

import?java.io.*;

import?java.util.*;

public?class?Main?{

public?static?void?main(String[]?args)?throws?Exception?{

getDelete("D:\\a.txt");

}

private?static?void?getDelete(String?pathName)throws?Exception{

File?file?=?new?File(pathName);

if(!file.exists())

throw?new?RuntimeException("異常:文件不存在!");

BufferedReader?br?=?new?BufferedReader(new?FileReader(file));

ListString?list?=?new?ArrayListString();

String?line?=?"";

while((line=br.readLine())!=null){

if((line.indexOf("AA")!=-1)?||?(line.indexOf("BB")!=-1))

list.add(line);

}

br.close();

getWriter(pathName,list);

}

private?static?void?getWriter(String?pathName,ListString?list)throws?Exception{

BufferedWriter?bw?=?new?BufferedWriter(new?FileWriter(pathName));

for(int?i?=?0;?i??list.size();?i++){

bw.write(list.get(i));

bw.newLine();

}

bw.close();

}

}

//測(cè)試文件內(nèi)容:

AAasdasdsad

akldjalfd

fdgjdlkjh

gfhljlh

sdjfsdfBBsdflskdf

BBsdfsdf

asdkjlkg

dfkgjdfklgjdfg

AA

//最后測(cè)試文件結(jié)果:

AAasdasdsad

sdjfsdfBBsdflskdf

BBsdfsdf

AA

本文題目:dsa代碼java dsl代碼
網(wǎng)站網(wǎng)址:http://muchs.cn/article44/doscjhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、用戶體驗(yàn)、微信公眾號(hào)靜態(tài)網(wǎng)站、Google動(dòng)態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司