Android Question declared value

Almora

Active Member
Licensed User
Longtime User
I used this code to give a lot of value, but it gives an error. I think I'm doing it wrong. How do I fix this? Is there a practical way?

B4X:
    Dim ka(160) As Int


    ka(0)="www.a..."
    ka(1)="www.b..."
    ka(2)="www.c..."
    .....
    ka(160)="www.xx..."

   

    WebView1.LoadHtml(ka(0))
    WebView1.LoadHtml(ka(1))
    WebView1.LoadHtml(ka(2))
    ....
    WebView1.LoadHtml(ka(160))
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Almora

Active Member
Licensed User
Longtime User
B4X:
Dim ka(160) As String

Dim WebView1 as WebView
Dim WebView2 as WebView
Dim WebView3 as WebView



    ka(0)="www.a..."
    ka(1)="www.b..."
    ka(2)="www.c..."
    .....
    ka(160)="www.xx..."


  if Main1.a=0 Then
    WebView1.LoadHtml(ka(0))
    WebView2.LoadHtml(ka(1))
    WebView3.LoadHtml(ka(2))

 Else If Main1.a=1 Then
    WebView1.LoadHtml(ka(3))
    WebView2.LoadHtml(ka(4))
    WebView3.LoadHtml(ka(5))

Else If Main1.a=2 Then
    WebView1.LoadHtml(ka(6))
    WebView2.LoadHtml(ka(7))
    WebView3.LoadHtml(ka(8))

...........



End If

error code:
java.lang.ArrayIndexOutOfBoundsException: length=160; index=160
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

Almora

Active Member
Licensed User
Longtime User
I would go to the problem of research.
this work..


B4X:
Dim ka(161) as String

For i =0 To ka.Lenght-1
...
Next
...

Don..:cool:
 
Last edited:
Upvote 0

Almora

Active Member
Licensed User
Longtime User
I think this is a better solution ..

B4X:
Dim ka() As String = Array As String( _
    "", _
    "website 1", _
    "website 2" _
)

Dim NumWebsites As Int = ka.Length - 1    '1-based array, numbered 1..NumWebSites

For I = 1 To NumWebsites
    WebView1.LoadHtml(ka(I))
Next
 
Upvote 0

emexes

Expert
Licensed User
I think the 0's have it! :)
I think that table was compiled by someone with a bias towards 0, rather than no bias. It looks like there is a battle about whether arrays should start at 0 or 1, and the combatants have completely missed the question of: why even have this constraint at all?

Every BASIC compiler I've used since VAX BASIC in the '80s has allowed things like Dim StudentsInYearLevel(7 to 12) or SuperBowlChampion(1967 to CurrentYear). Pascal too, and that language isn't even on the list (wtf?). And I'm pretty sure FORTRAN did too, probably even earlier.

Better yet, indices can be negative, eg: Dim DirectionNSEW(-1 to 1, -1 to 1) Pascal could even apply range-checking to integer (maybe even scalar) variables, thus you could define Temperature as being -273..101, or EggsInCarton as 0..12, or DiceTotal as 2..12.

But... but... but... 0-based arrays are more efficient. Agreed. But Java is doing runtime bounds checks on array accesses anyway, so clearly factors other than efficiency were considered. Like making the programmer's life easier :) So if we're already spending CPU cycles on calculating and checking array indexes, the cost of an extra subtraction is negligible in comparison to the benefit of array bounds that match the problem being solved rather than conforming to theoretical constraints.

Better yet, you could have your cake and eat it too, eg, per PowerBASIC documentation:
While PowerBASIC supports lower boundary values that are non-zero, PowerBASIC generates the most efficient code if the lower boundary parameter is omitted (i.e., the array uses the default lower boundary of zero).
The whole discussion is a bit moot with regards to B4A, though, since it's built upon Java, interacts with Java libraries, using Java arrays, so: 0-based it is. At least we have the freedom to emulate 1-based natural-numbered arrays by dimming one more than we need and then ignoring element zero. All is good, as long as you don't post the code in public...

;-)
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
All is good, as long as you don't post the code in public...
;-)

If you want to post code that uses non-0 based arrays that's up to you, but in this case the OP didn't seem to understand the implications of a 0-based array & others were trying to point out where he was going wrong.

I see a lot of code posted that (in my mind) is not written "properly", but generally members of the forum limit their responses to addressing the issue rather than making comments on how the code is written. The few exceptions I've seen to this have been when the code is so bad that even if the primary issue is resolved, it still won't work as expected. I used to be nervous about posting my own code in public, but the reality is that there is always more than 1 way to skin a cat (a prime example being looping through array elements :)), so as long as a piece of code achieves the desired result you can't really say that it's "wrong". (just my 2c worth)

Back on-topic (sort of): I think the best explanation I've seen on why 0-based arrays are 0-based is that index isn't really an index - it's an offset from the first element of the array, therefore the first element offset is 0 & the last element offset is array size - 1.

- Colin.
 
Upvote 0

emexes

Expert
Licensed User
always more than 1 way to skin a cat ... so as long as a piece of code achieves the desired result you can't really say that it's "wrong".
:) great minds think alike

I think the best explanation I've seen on why 0-based arrays are 0-based is that index isn't really an index - it's an offset from the first element of the array, therefore the first element offset is 0 & the last element offset is array size - 1.
For one dimension, agreed.

edit: remainder of post deleted (subsumed by cat-skinning idiom ;-)
 
Last edited:
Upvote 0
Top