D Declan Well-Known Member Licensed User Longtime User Apr 4, 2017 #1 I have a String Array that I am creating as follows: Code: Dim booknames() As String = Array As String("text_creative_arts.epub", _ "text_english_reader.epub", _ "text_Life_Orientation.epub", _ "text_mathematics.epub") I wish to now create this Array "on-the-fly" by reading the files within the folder. I can create a List of files, as follows: Code: Dim ImageList As List Dim ImageCount As Int ImageList = File.ListFiles("/storage/emulated/0/books/Text/") ImageCount = ImageList.Size Log("File List = " & ImageList) Log("File Count = " & ImageCount) The Log is: Code: File List = (ArrayList) [text_creative_arts.epub, text_english_reader.epub, text_Life_Orientation.epub, text_mathematics.epub] File Count = 4 How do I create the Array for: Code: Dim booknames() As String = Array As String()
I have a String Array that I am creating as follows: Code: Dim booknames() As String = Array As String("text_creative_arts.epub", _ "text_english_reader.epub", _ "text_Life_Orientation.epub", _ "text_mathematics.epub") I wish to now create this Array "on-the-fly" by reading the files within the folder. I can create a List of files, as follows: Code: Dim ImageList As List Dim ImageCount As Int ImageList = File.ListFiles("/storage/emulated/0/books/Text/") ImageCount = ImageList.Size Log("File List = " & ImageList) Log("File Count = " & ImageCount) The Log is: Code: File List = (ArrayList) [text_creative_arts.epub, text_english_reader.epub, text_Life_Orientation.epub, text_mathematics.epub] File Count = 4 How do I create the Array for: Code: Dim booknames() As String = Array As String()
DonManfred Expert Licensed User Longtime User Apr 4, 2017 #2 write a small sub which get the list as parameter and which will create the right array for you with the content of the list and return the newly created array Upvote 0
write a small sub which get the list as parameter and which will create the right array for you with the content of the list and return the newly created array
M Mahares Expert Licensed User Longtime User Apr 4, 2017 #3 Code: Dim booknames(ImageCount) As String For i=0 To ImageCount -1 booknames(i)= ImageList.get(i) Next Upvote 0
Code: Dim booknames(ImageCount) As String For i=0 To ImageCount -1 booknames(i)= ImageList.get(i) Next
klaus Expert Licensed User Longtime User Apr 4, 2017 #4 Instead of transforming the List to an Array why not use List for both? The code in the first post would become: Code: lstBookNames.AddAll(Array As String("text_creative_arts.epub", _ "text_english_reader.epub", _ "text_Life_Orientation.epub", _ "text_mathematics.epub")) And then access them with lstBookNames.Get(i) Upvote 0
Instead of transforming the List to an Array why not use List for both? The code in the first post would become: Code: lstBookNames.AddAll(Array As String("text_creative_arts.epub", _ "text_english_reader.epub", _ "text_Life_Orientation.epub", _ "text_mathematics.epub")) And then access them with lstBookNames.Get(i)
stevel05 Expert Licensed User Longtime User Apr 4, 2017 #5 ArrayList (https://developer.android.com/reference/java/util/ArrayList.html#toArray()) has a toArray Method that you can access via JavaObject: Code: Dim L As List = File.ListFiles(File.DirRootExternal) Dim Arr(L.Size) As String = AsJO(L).RunMethod("toArray",Null) For i = 0 To Arr.Length - 1 Log(Arr(i)) Next Private Sub AsJO(Jo As JavaObject) As JavaObject Return Jo End Sub I'm not sure that it will give any noticeable performance gains over doing it yourself in a loop, or using the List directly , but it's there as an option. Upvote 0
ArrayList (https://developer.android.com/reference/java/util/ArrayList.html#toArray()) has a toArray Method that you can access via JavaObject: Code: Dim L As List = File.ListFiles(File.DirRootExternal) Dim Arr(L.Size) As String = AsJO(L).RunMethod("toArray",Null) For i = 0 To Arr.Length - 1 Log(Arr(i)) Next Private Sub AsJO(Jo As JavaObject) As JavaObject Return Jo End Sub I'm not sure that it will give any noticeable performance gains over doing it yourself in a loop, or using the List directly , but it's there as an option.
Erel B4X founder Staff member Licensed User Longtime User Apr 4, 2017 #6 Why do you want to use an array instead of a List? Upvote 0