程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java list與數組之間的轉換具體解析

java list與數組之間的轉換具體解析

編輯:關於JAVA

java list與數組之間的轉換具體解析。本站提示廣大學習愛好者:(java list與數組之間的轉換具體解析)文章只能為提供參考,不一定能成為您想要的結果。以下是java list與數組之間的轉換具體解析正文


1 數組轉換為List
挪用Arrays類的靜態辦法asList。

asList
public static <T> List<T> asList(T... a)Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray(). The returned list is serializable and implements RandomAccess.
This method also provides a convenient way to create a fixed-size list initialized to contain several elements:

List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

Parameters:
a - the array by which the list will be backed
Returns:
a list view of the specified array

用法:API中供給了一種應用的辦法。更加經常使用的示例代碼:

String[] arr = new String[] {"str1", "str2"};
List<String> list = Arrays.asList(arr);

2 List轉換為數組
這裡的List以ArrayList為例,ArrayList的API供給了兩種可供應用的函數。

toArray
public Object[] toArray()Returns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Returns:
an array containing all of the elements in this list in proper sequence
See Also:
Arrays.asList(Object[])

--------------------------------------------------------------------------------
toArray
public <T> T[] toArray(T[] a)Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Overrides:
toArray in class AbstractCollection<E>
Parameters:
a - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of the list
Throws:
ArrayStoreException - if the runtime type of the specified array is not a supertype of the runtime type of every element in this list
NullPointerException - if the specified array is null

用法:示例代碼:

List<String> list = new ArrayList<String>();
list.add("str1");
list.add("str2");
int size = list.size();
String[] arr = (String[])list.toArray(new String[size]);//應用了第二種接口,前往值和參數均為成果

�有關於此的具體信息,我們的劇本診所 專欄對此主題停止了深刻論述。如今,要解釋的只是我們將應用文本文件 C:\Scripts\Test.txt,我們經由過程為變量 strPathToTextFile 和 strFile 付與響應值來表現:

strPathToTextFile = "C:\Scripts\"
strFile = "Test.txt"

那末,這若何能讓我們除去反復行呢?是如許的,有一種稱為 Select DISTINCT 的數據庫查詢;應用 Select DISTINCT 可以選擇表格中一切分歧的(或獨一的)記載。假定您有一個簡略的數據庫,個中有以下記載:


Red
Red
Blue
Red

假如應用 Select DISTINCT 查詢,您將獲得一個只包含獨一記載的記載集:

Red
Blue

毫無疑問,您會想:“哇!前往獨一記載與刪除反復記載的確異曲同工。”我們認可確切如斯 – 嗯,請等一下:您的設法主意相對准確。我們的文本文件構建得就像一個數據庫表,文本文件中的每行都表現一筆記錄中的一個字段。假如對此文本文件運轉 Select DISTINCT 查詢,我們將只獲得獨一的行。現實上,我們將獲得以下所示的記載集:

This is one of the lines in the text file.
This is another line in the text file.
This is yet another line in the text file.

這恰好就是我們願望前往的信息。您為我們指出了這一點,這很好!

檢索記載集後,我們再應用以下代碼將獨一的行回顯到屏幕:

Do Until objRecordset.EOF
    Wscript.Echo objRecordset.Fields.Item(0).Value   
    objRecordset.MoveNext
Loop

假如我們情願,也能夠應用 FileSystemObject 翻開文本文件,然後僅用獨一的行調換現有內容;此種辦法與從文本文件中刪除一切反復行後果雷同。(假如我們能應用某種 Update 查詢履行此操作,後果會很好,但處置文本文件時,ADO 倒是只讀的。)

那末,這是從文本文件刪除反復項(不管是姓名照樣全部行)的終究結論嗎?唉,誰曉得:究竟,永無盡頭的尋覓進程須要時光!(現實上,我們發明這只須要年夜約 2 到 3 天。然後,我們便開端認為無聊,又持續做其他工作。)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved