锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
錛堜竴錛夌Щ闄よ妭鐐瑰強灞炴?/strong>
杈撳嚭緇撴灉涓猴細 1銆傛紜殑鍒犻櫎浜嗙被鍨嬩負society鐨刡ook鑺傜偣
鍒嗘瀽錛?/strong>
絎簩涓緭鍑虹粨鏋滀笉鑳藉垹闄ex鑺傜偣錛屾垜浠渶瑕佺湅dom4j鐨凙PI 浠庝腑鎴戜滑鍙互鐪嬪嚭錛宺emove鍙兘鐢ㄥ湪瀹冭嚜宸辯殑鐩存帴瀛╁瓙鑺傜偣涓婏紝涓嶈兘鐢ㄥ湪瀛欏瓙鑺傜偣涓婏紝鍥犱負sex鑺傜偣涓嶆槸root鑺傜偣鐨勭洿鎺ュ瀛愯妭鐐癸紝鎵浠ヤ笉鑳藉垹闄わ紱鑰宻ex鑺傜偣鍗存槸author鑺傜偣鐨勭洿鎺ュ瀛愯妭鐐癸紝鎵浠ョ涓変釜杈撳嚭鍙互鍒犻櫎銆?/p>
錛堜簩錛夊皢涓や釜Document鍚堝茍涓轟竴涓狣ocument
鍏堢湅涓涓敊璇殑鎯呭喌 錛?錛変嬌鐢╝dd()鏂規硶娣誨姞 璋冪敤CombineDocument鍑芥暟錛屼細鍑虹幇浠ヤ笅閿欒錛?/p>
org.dom4j.IllegalAddException: The node "org.dom4j.tree.DefaultElement@17bd6a1 [Element: <author attributes: []/>]" could not be added to the element "root" because: The Node already has an existing parent of "root" 鍗蟲彁紺篴uthor鑺傜偣宸茬粡鏈変竴涓猺oot鑺傜偣浜嗭紝涓嶈兘鍐嶆坊鍔犲埌鍙︿竴涓妭鐐逛笂鍘匯?/p>
錛?錛変嬌鐢╝ppendContent()鏂規硶 鍗沖皢doc_book.getRootElement().add(author); 鏀逛負錛歞oc_book.getRootElement().appendContent(author); 杈撳嚭緇撴灉涓猴細 <?xml version="1.0" encoding="UTF-8"?> 鍙互鐪嬪嚭錛岀己灝戜簡author鑺傜偣錛屽彧鏄妸author鑺傜偣鐨勫瓙鑺傜偣娣誨姞涓婂幓浜嗭紝浣嗘槸鐢辨鍙錛宎ppendContent鏂規硶鏄湁甯屾湜鐨勩?/p>
鎴戜滑鐪嬩竴涓媎om4j鐨凙PI: 錛?錛変嬌鐢ㄦ紜殑appendContent鏂規硶 灝嗭細Element author=(Element)doc_author.selectSingleNode("http://author"); doc_book.getRootElement().appendContent(author); 鏀逛負錛歞oc_book.getRootElement().appendContent(doc_author.getRootElement()); 杈撳嚭錛?/p>
<?xml version="1.0" encoding="UTF-8"?> 鏄紜粨鏋?/p>
錛?錛夊彟涓縐嶅彲琛岀殑鏂規硶
錛堜竴錛夊垱寤篋ocument鐨勫熀鏈搷浣?/strong>
/** 璋冪敤BaseOperation錛岃緭鍑虹粨鏋滀負錛?/p>
<?xml version="1.0" encoding="UTF-8"?>
錛堜簩錛夋牴鎹竴涓鍚圖ocument鏍煎紡鐨勫瓧絎︿覆鏉ョ敓鎴愪竴涓狣ocument
聽/**灝嗗瓧絎︿覆杞寲涓篋ocument 璋冪敤紺轟緥錛?/p>
String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>"; 聽聽Document document = parserStrtoDocument(str); 杈撳嚭緇撴灉涓猴細 <?xml version="1.0" encoding="UTF-8"?>
錛堜笁錛夊彇寰梮ml鑺傜偣灞炴х殑鍩烘湰鏂規硶
聽/** 璋冪敤getBaseInfofromDocument錛岃緭鍑虹粨鏋滀負錛?/p>
涔﹀悕錛欽ava
錛堝洓錛夊埄鐢ㄨ凱浠o紝xpath鍙栧緱鑺傜偣鍙婂叾灞炴у?/strong>
聽/**鍒╃敤榪唬錛寈path鍙栧緱xml鐨勮妭鐐瑰強鍏跺睘鎬у?br />聽 * @throws DocumentException
聽聽//鏌ユ壘浣滆呭鍚?br />聽聽Element author=(Element)document.selectSingleNode("http://author"); 璋冪敤getComplexInfofromDocument錛岃緭鍑虹粨鏋滀負錛?/p>
<book type="society"><Name>Society security</Name><price>130</price></book>
澶囨敞錛氳皟鐢ㄨ鏂規硶涔嬪墠錛屽簲璇ュ厛鍚戝伐紼嬩腑娣誨姞鏀寔xpath鐨刯ar鍖咃紝鍚﹀垯錛屼細鍑虹幇浠ヤ笅閿欒錛?/strong>
java.lang.NoClassDefFoundError: org/jaxen/JaxenException 鍙渶瑕佸紩鍏axen鍖呭氨琛屼簡錛屾垜浣跨敤鐨勬槸hibernate鍖呬腑鐨刯axen-1.1-beta-7.jar鍖呫?/p>
]]>
聽聽聽聽
/**聽*/
/**
縐婚櫎鑺傜偣鍜屽睘鎬х殑鎿嶄綔
聽聽聽聽聽*聽
@throws
聽DocumentException
聽聽聽聽聽
*/
聽聽聽聽
public
聽
void
聽RemoveOperator()聽
throws
聽DocumentException
...
{
聽聽聽聽聽聽聽聽
//
寰呯敓鎴恱ml鐨勫瓧絎︿覆
聽聽聽聽聽聽聽聽String聽str
=
"
<root><book聽type='science'><Name>Java</Name><price>100</price></book>
"
聽聽聽聽聽聽聽聽聽聽聽聽
+
"
<book聽type='society'><Name>Society聽security</Name><price>130</price></book>
"
聽聽聽聽聽聽聽聽聽聽聽聽
+
"
<author><name>chb</name><sex>boy</sex></author></root>
"
;
聽聽聽聽聽聽聽聽
//
鐢熸垚涓涓狣ocument
聽聽聽聽聽聽聽聽Document聽document聽
=
聽DocumentHelper.parseText(str);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽Element聽root
=
document.getRootElement();
聽聽聽聽聽聽聽聽
//
鍒犻櫎綾誨瀷涓簊ociety鐨刡ook鑺傜偣
聽聽聽聽聽聽聽聽Element聽book_society
=
(Element)document.selectSingleNode(
"
//book[@type='society']
"
);
聽聽聽聽聽聽聽聽root.remove(book_society);
聽聽聽聽聽聽聽聽System.out.println(
"
1銆傛紜殑鍒犻櫎浜嗙被鍨嬩負society鐨刡ook鑺傜偣
"
);
聽聽聽聽聽聽聽聽System.out.println(document.asXML());
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//
鍒犻櫎sex鑺傜偣
聽聽聽聽聽聽聽聽Element聽sex
=
(Element)root.selectSingleNode(
"
//sex
"
);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//
浠巖oot鑺傜偣鍒犻櫎
聽聽聽聽聽聽聽聽root.remove(sex);
聽聽聽聽聽聽聽聽System.out.println(
"
2銆傝繖鏍鋒槸涓嶈兘鍒犻櫎sex鑺傜偣鐨?/span>
"
);
聽聽聽聽聽聽聽聽System.out.println(document.asXML());
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//
浠巃uthor鑺傜偣鍒犻櫎
聽聽聽聽聽聽聽聽root.element(
"
author
"
).remove(sex);
聽聽聽聽聽聽聽聽System.out.println(
"
3銆傝繖鏍峰氨鍙互姝g‘鍒犻櫎sex鑺傜偣
"
);
聽聽聽聽聽聽聽聽System.out.println(document.asXML());
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//
鍒犻櫎灞炴?/span>
聽聽聽聽聽聽聽聽Attribute聽type
=
root.element(
"
book
"
).attribute(
"
type
"
);
聽聽聽聽聽聽聽聽root.element(
"
book
"
).remove(type);
聽聽聽聽聽聽聽聽System.out.println(
"
4銆傛紜垹闄ook鑺傜偣鐨則ype灞炴?/span>
"
);
聽聽聽聽聽聽聽聽System.out.println(document.asXML());
聽聽聽聽}
<?xml version="1.0" encoding="UTF-8"?>
<root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name><sex>boy</sex></author></root>
2銆傝繖鏍鋒槸涓嶈兘鍒犻櫎sex鑺傜偣鐨?br /><?xml version="1.0" encoding="UTF-8"?>
<root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name><sex>boy</sex></author></root>
3銆傝繖鏍峰氨鍙互姝g‘鍒犻櫎sex鑺傜偣
<?xml version="1.0" encoding="UTF-8"?>
<root><book type="science"><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>
4銆傛紜垹闄ook鑺傜偣鐨則ype灞炴?br /><?xml version="1.0" encoding="UTF-8"?>
<root><book><Name>Java</Name><price>100</price></book><author><name>chb</name></author></root>remove
public boolean remove(Element聽element)
Element
if the node is an immediate child of this branch. If the given node is not an immediate child of this branch then the Node.detach()
method should be used instead.
element
- is the element to be removed
public
聽
void
聽CombineDocument()聽
throws
聽DocumentException
...
{
聽聽聽聽聽聽聽聽
//
寰呯敓鎴愪袱涓狣ocument鐨勫瓧絎︿覆
聽聽聽聽聽聽聽聽String聽str_book
=
"
<root><book聽type='science'><Name>Java</Name><price>100</price></book>
"
聽聽聽聽聽聽聽聽聽聽聽聽
+
"
<book聽type='society'><Name>Society聽security</Name><price>130</price></book>
"
聽聽聽聽聽聽聽聽聽聽聽聽
+
"
</root>
"
;
聽聽聽聽聽聽聽聽String聽str_author
=
"
<root><author><name>chb</name><sex>boy</sex></author></root>
"
;
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//
鐢熸垚涓や釜Document
聽聽聽聽聽聽聽聽Document聽doc_book
=
DocumentHelper.parseText(str_book);
聽聽聽聽聽聽聽聽Document聽doc_author
=
DocumentHelper.parseText(str_author);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//
鍙栧嚭doc_author鐨刟uthor鑺傜偣錛屾坊鍔犲埌doc_book鐨勬牴緇撶偣
聽聽聽聽聽聽聽聽Element聽author
=
(Element)doc_author.selectSingleNode(
"
//author
"
);
聽聽聽聽聽聽聽聽doc_book.getRootElement().add(author);
聽聽聽聽聽聽聽聽System.out.println(doc_book.asXML());
聽聽聽聽}
聽at org.dom4j.tree.AbstractElement.addNode(AbstractElement.java:1521)
聽at org.dom4j.tree.AbstractElement.add(AbstractElement.java:1002)
聽at xml_chb.dom4j_chb.CombineDocument(dom4j_chb.java:189)
聽at xml_chb.dom4j_chb.main(dom4j_chb.java:199)
Exception in thread "main"
<root>
<book type="science"><Name>Java</Name><price>100</price></book>
<book type="society"><Name>Society security</Name><price>130</price></book>
<name>chb</name><sex>boy</sex>
</root>appendContent
public void appendContent(Branch聽branch)
Collection.addAll(java.util.Collection)
method.
branch
- is the branch whose content will be added to me.
<root>
<book type="science"><Name>Java</Name><price>100</price></book>
<book type="society"><Name>Society security</Name><price>130</price></book>
<author><name>chb</name><sex>boy</sex></author>
</root>
public
聽
void
聽CombineDocument()聽
throws
聽DocumentException
...
{
聽聽聽聽聽聽聽聽
//
寰呯敓鎴愪袱涓狣ocument鐨勫瓧絎︿覆
聽聽聽聽聽聽聽聽String聽str_book
=
"
<root><book聽type='science'><Name>Java</Name><price>100</price></book>
"
聽聽聽聽聽聽聽聽聽聽聽聽
+
"
<book聽type='society'><Name>Society聽security</Name><price>130</price></book>
"
聽聽聽聽聽聽聽聽聽聽聽聽
+
"
</root>
"
;
聽聽聽聽聽聽聽聽String聽str_author
=
"
<root><author><name>chb</name><sex>boy</sex></author></root>
"
;
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//
鐢熸垚涓や釜Document
聽聽聽聽聽聽聽聽Document聽doc_book
=
DocumentHelper.parseText(str_book);
聽聽聽聽聽聽聽聽Document聽doc_author
=
DocumentHelper.parseText(str_author);
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽
//
鏂扮敓鎴愪竴涓狣ocument
聽聽聽聽聽聽聽聽Element聽author
=
DocumentHelper.createElement(
"
author
"
);
聽聽聽聽聽聽聽聽author.appendContent((Element)doc_author.selectSingleNode(
"
//author
"
));
聽聽聽聽聽聽聽聽
//
褰撳墠author灝氭棤鐖惰妭鐐癸紝鎵浠ュ彲浠ヤ嬌鐢╝dd鏂規硶娣誨姞
聽聽聽聽聽聽聽聽doc_book.getRootElement().add(author);
聽聽聽聽聽聽聽聽System.out.println(doc_book.asXML());
聽聽聽聽}
]]>
聽 * xml鍩烘湰鎿嶄綔
聽 */
聽public void BaseOperation(){
聽聽//鍒涘緩涓涓猟ocument
聽聽Document document=DocumentHelper.createDocument();
聽聽//鍒涘緩鏍圭粨鐐?br />聽聽Element root=document.addElement("root");
聽聽//涓烘牴緇撶偣娣誨姞涓涓猙ook鑺傜偣
聽聽Element book1=root.addElement("book");
聽聽//涓篵ook1娣誨姞灞炴ype
聽聽book1.addAttribute("type","science");
聽聽//涓篵ook1娣誨姞name瀛愯妭鐐?br />聽聽Element name1=book1.addElement("Name");
聽聽//騫惰緗叾name涓?Java"
聽聽name1.setText("Java");
聽聽//涓篵ook1鍒涘緩涓涓猵rice鑺傜偣,騫惰鍏朵環鏍間負100
聽聽book1.addElement("price").setText("100");
聽聽
聽聽//涓烘牴緇撶偣娣誨姞絎簩涓猙ook鑺傜偣錛屽茍璁劇疆璇ook鑺傜偣鐨則ype灞炴?br />聽聽Element book2=root.addElement("book").addAttribute("type","science");
聽聽//涓篵ook1娣誨姞name瀛愯妭鐐?br />聽聽Element name2=book2.addElement("Name");
聽聽//騫惰緗叾name涓?Oracle"
聽聽name2.setText("Oracle");
聽聽//涓篵ook1鍒涘緩涓涓猵rice鑺傜偣,騫惰鍏朵環鏍間負200
聽聽book2.addElement("price").setText("200");
聽聽
聽聽//杈撳嚭xml
聽聽System.out.println(document.asXML());
聽}
<root>
聽<book type="science">
聽聽<Name>Java</Name>
聽聽<price>100</price>
聽</book>
聽<book type="science">
聽聽<Name>Oracle</Name>
聽聽<price>200</price>
聽</book>
</root>
聽 * @param str聽 杈撳叆鐨勫瓧絎︿覆
聽 * @return聽 鐢熸垚鐨刣ocument
聽 * @throws DocumentException
聽 */
聽public Document parserStrtoDocument(String str) throws DocumentException{
聽聽Document document=DocumentHelper.parseText(str);
聽聽return document;
聽}
聽聽System.out.println(document.asXML());
<root>
聽<book type="science">
聽聽<Name>Java</Name>
聽聽<price>100</price>
聽</book>
</root>
聽 * 鍙栧緱xml鐨勮妭鐐瑰拰灞炴х殑鍊?br />聽 * @throws DocumentException
聽 */
聽public void getBaseInfofromDocument() throws DocumentException{
聽聽String str="<root><book type='science'><Name>Java</Name><price>100</price></book></root>";
聽聽//鐢熸垚涓涓狣ocument
聽聽Document document = DocumentHelper.parseText(str);
聽聽//鍙栧緱鏍圭粨鐐?br />聽聽Element root=document.getRootElement();
聽聽//鍙栧緱book鑺傜偣
聽聽Element book=root.element("book");
聽聽//鍙栧緱book鑺傜偣鐨則ype灞炴х殑鍊?br />聽聽String type=book.attributeValue("type");
聽聽//鍙栧緱Name鑺傜偣
聽聽Element name=book.element("Name");
聽聽//鍙栧緱涔﹀悕
聽聽String bookname=name.getText();
聽聽//鍙栧緱涔︾殑浠烽挶
聽聽int price=Integer.parseInt(book.element("price").getText());
聽聽
聽聽//杈撳嚭涔︾洰淇℃伅
聽聽System.out.println("涔﹀悕錛?+bookname);
聽聽System.out.println("鎵灞炵被鍒細"+type);
聽聽System.out.println("浠鋒牸錛?+price);
聽}
鎵灞炵被鍒細science
浠鋒牸錛?00
聽 */
聽public void getComplexInfofromDocument() throws DocumentException{
聽聽String str="<root><book type='science'><Name>Java</Name><price>100</price></book>"
聽聽聽+"<book type='science'><Name>Oracle</Name><price>120</price></book>"
聽聽聽+"<book type='society'><Name>Society security</Name><price>130</price></book>"
聽聽聽+"<author><name>chb</name></author></root>";
聽聽//鐢熸垚涓涓狣ocument
聽聽Document document = DocumentHelper.parseText(str);
聽聽
聽聽//鎻愬彇綾誨瀷涓?society"鐨勪功
聽聽//姝ゅ闇瑕佹坊鍔犳敮鎸亁path鐨刯ar鍖咃紝璇︾粏瑙佸娉?br />聽聽Element society_book=(Element)document.selectSingleNode("/root/book[@type='society']");
聽聽System.out.println(society_book.asXML());
聽聽
聽聽//鎻愬彇浠鋒牸鑺傜偣鐨勫垪琛?br />聽聽System.out.println("-----------浠鋒牸鍒楄〃-------------");
聽聽List price=document.selectNodes("http://price");
聽聽for(int i=0;i<price.size();i++){
聽聽聽Element elem_price=(Element)price.get(i);
聽聽聽System.out.println(elem_price.getText());
聽聽}
聽聽
聽聽//寰幆鏍圭粨鐐逛笅鐨勬墍鏈夎妭鐐癸紝鑻ュ綋鍓嶈妭鐐逛負book錛屽垯杈撳嚭榪欐湰涔︾殑璇︾粏淇℃伅
聽聽System.out.println("-------------涔︾洰璇︽儏------------");
聽聽System.out.println("涔﹀悕\t\t綾誨埆\t\t浠鋒牸");
聽聽Element root=document.getRootElement();
聽聽Iterator iterator=root.elementIterator();
聽聽while(iterator.hasNext()){
聽聽聽Element element=(Element)iterator.next();
聽聽聽if(element.getName().equals("book")){
聽聽聽聽System.out.print(element.element("Name").getText()+"\t");
聽聽聽聽System.out.print(element.attributeValue("type")+"\t\t");
聽聽聽聽System.out.print(element.element("price").getText()+"\n");
聽聽聽}
聽聽}
聽聽System.out.println("---------"+author.element("name").getText()+"----------");
聽聽//鎻愬彇浣滆呯殑鎵鏈変功鐩悕縐?br />聽聽Iterator iterator_book=root.elementIterator("book");聽聽
聽聽while(iterator_book.hasNext()){
聽聽聽Element book=(Element)iterator_book.next();
聽聽聽System.out.print(book.element("Name").getText()+"\t");
聽聽}
聽聽
聽聽//灞炴ц凱浠?br />聽聽System.out.println("\n-------灞炴ц凱浠?-------");
聽聽String str1="<book type='science' name='Java' price='100'/>";
聽聽Document document1=DocumentHelper.parseText(str1);
聽聽//寮濮嬭凱浠?br />聽聽Iterator iterator_attribute=document1.getRootElement().attributeIterator();
聽聽while(iterator_attribute.hasNext()){
聽聽聽//鎻愬彇褰撳墠灞炴?br />聽聽聽Attribute attribute=(Attribute)iterator_attribute.next();
聽聽聽System.out.println(attribute.getName()+":"+attribute.getValue());
聽聽}
聽}
-----------浠鋒牸鍒楄〃-------------
100
120
130
-------------涔︾洰璇︽儏------------
涔﹀悕聽聽綾誨埆聽聽浠鋒牸
Java聽science聽聽100
Oracle聽science聽聽120
Society security聽society聽聽130
---------chb----------
Java聽Oracle聽Society security聽
-------灞炴ц凱浠?-------
type:science
name:Java
price:100
聽at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
聽at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
聽at org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:183)
聽at xml_chb.dom4j_chb.getComplexInfofromDocument(dom4j_chb.java:82)
聽at xml_chb.dom4j_chb.main(dom4j_chb.java:92)
Exception in thread "main"
聽聽聽 聽聽聽 Hashtable<String, String> hash=new Hashtable<String, String>();
聽聽聽 聽聽聽 for(int i=0;i<str.length;i++){
聽聽聽 聽聽聽 聽聽聽 if(!hash.containsKey(str[i]))
聽聽聽 聽聽聽 聽聽聽 聽聽聽 hash.put(str[i], str[i]);
聽聽聽 聽聽聽 }
聽聽聽 聽聽聽 Enumeration enumeration=hash.keys();
聽聽聽 聽聽聽 String[] str_new=new String[hash.size()];
聽聽聽 聽聽聽 int i=0;
聽聽聽 聽聽聽 while(enumeration.hasMoreElements()){
聽聽聽 聽聽聽 聽聽聽 str_new[i]=enumeration.nextElement().toString();
聽聽聽 聽聽聽 聽聽聽 i++;
聽聽聽 聽聽聽 }
聽聽聽 聽聽聽 return str_new;
聽聽聽 }
紺轟緥錛?br />聽聽聽 聽聽聽 String[] mobile={"13811071500","13811071500","13811071501","13811071503","13811071501"};
聽聽聽 聽聽聽 mobile=checkArray(mobile);
聽聽聽 聽聽聽 for(int i=0;i<mobile.length;i++)
聽聽聽 聽聽聽 聽聽聽 System.out.println(mobile[i]);
聽聽聽 聽聽 杈撳嚭緇撴灉涓猴細
聽聽聽 聽聽聽 13811071503
聽聽聽 聽聽聽 13811071501
聽聽聽 聽聽聽 13811071500
2.A,B鍧囦負瀛楃涓叉暟緇勶紝鎵懼嚭鍦ˋ涓瓨鍦紝鑰屽湪B涓笉瀛樺湪鐨勫瓧絎︿覆
聽聽聽 public String[] compareArray(String[] A,String[] B){
聽聽聽 聽聽聽 Hashtable<String, String> hash=new Hashtable<String, String>();
聽聽聽 聽聽聽 Hashtable<String, String> hash_new=new Hashtable<String, String>();
聽聽聽 聽聽聽 for(int i=0;i<B.length;i++)
聽聽聽 聽聽聽 聽聽聽 hash.put(B[i], B[i]);
聽聽聽 聽聽聽 for(int i=0;i<A.length;i++){
聽聽聽 聽聽聽 聽聽聽 if(!hash.containsKey(A[i]))
聽聽聽 聽聽聽 聽聽聽 聽聽聽 hash_new.put(A[i], A[i]);
聽聽聽 聽聽聽 }
聽聽聽 聽聽聽 String[] C=new String[hash_new.size()];
聽聽聽 聽聽聽 int i=0;
聽聽聽 聽聽聽 Enumeration enumeration=hash_new.keys();
聽聽聽 聽聽聽 while(enumeration.hasMoreElements()){
聽聽聽 聽聽聽 聽聽聽 C[i]=enumeration.nextElement().toString();
聽聽聽 聽聽聽 聽聽聽 i++;
聽聽聽 聽聽聽 }
聽聽聽 聽聽聽 return C;
聽聽聽 }
紺轟緥錛?br />聽聽聽 聽聽聽 String[] mobile1={"13811071500","13811071501","13811071502","13811071503","13811071504"};
聽聽聽 聽聽聽 String[] mobile2={"13811071500","13811071505","13811071502","13811071506","13811071504"};
聽聽聽 聽聽聽 String[] mobile3=compareArray(mobile1,mobile2);
聽聽聽 聽聽聽 for(int i=0;i<mobile3.length;i++)
聽聽聽 聽聽聽 聽聽聽 System.out.println(mobile[i]);
杈撳嚭緇撴灉錛?br />聽聽聽 13811071503
聽聽聽 13811071501
瀛樺湪鐨勯棶棰橈細
姣忔閮芥槸鍊掑簭錛屽彲浠ュ啀瀵圭▼搴忕◢鍔犳敼鍔紝鍙樻垚姝e簭銆?/font>
]]>
1銆傚疄渚嬪寲瀵硅薄錛屽彲浠ョ敤濡備笅涓ょ鏂規硶錛?br />DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
鍥犱負DecimalFormat緇ф壙鑷狽umberFormat銆?br />2銆傝瀹氬皬鏁頒綅鏁?br />緋葷粺榛樿灝忔暟浣嶆暟涓?錛屽錛?br />聽聽DecimalFormat df=(DecimalFormat)NumberFormat.getInstance();
聽聽System.out.println(df.format(12.3456789));
杈撳嚭錛?2.346
鐜板湪鍙互閫氳繃濡備笅鏂規硶鎶婂皬鏁頒負璁句負涓や綅錛?br />聽 df.setMaximumFractionDigits(2);
聽聽System.out.println(df.format(12.3456789));
鍒欒緭鍑轟負錛?2.35
3銆傚皢鏁板瓧杞寲涓虹櫨鍒嗘瘮杈撳嚭錛屾湁濡備笅涓ょ鏂規硶錛?br />(1)
聽 df.applyPattern("##.##%");
聽聽System.out.println(df.format(12.3456789));
聽聽System.out.println(df.format(1));
聽聽System.out.println(df.format(0.015));
杈撳嚭鍒嗗埆涓猴細1234.57%聽 100%聽聽聽 1.5%
(2)
聽 df.setMaximumFractionDigits(2);
聽聽System.out.println(df.format(12.3456789*100)+"%");
聽聽System.out.println(df.format(1*100)+"%");
聽聽System.out.println(df.format(0.015*100)+"%");
杈撳嚭鍒嗗埆涓猴細
1,234.57%聽聽 100%聽聽 1.5%
4銆傝緗垎緇勫ぇ灝?br />聽聽 DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
聽聽df1.setGroupingSize(2);
聽聽System.out.println(df1.format(123456789));
杈撳嚭錛?,23,45,67,89
榪樺彲浠ラ氳繃df1.setGroupingUsed(false);鏉ョ鐢ㄥ垎緇勮緗紝濡傦細
聽聽 DecimalFormat df1=(DecimalFormat) DecimalFormat.getInstance();
聽聽df1.setGroupingSize(2);
聽聽df1.setGroupingUsed(false);
聽聽System.out.println(df1.format(123456789));
杈撳嚭錛?23456789
5銆傝緗皬鏁頒負蹇呴』涓?浣?br />聽 DecimalFormat df2=(DecimalFormat) DecimalFormat.getInstance();
聽聽df2.applyPattern("0.00");
聽聽System.out.println(df2.format(1.2));
杈撳嚭錛?.20