Multi Dimension Arrays, ArrayList, or List?

sterlingy

Active Member
Licensed User
Longtime User
Folks,

I've searched this forum and the documentation for what might be the best solution, but I'm only more confused now.

I have a database of questions. Each question has four possible answers, and one answer is correct.

A record might look like this:

Question, Answer1, Answer2, Answer3, Answer4, 3

The last field (3) tells us that the correct answer is Answer3

So...

I want to populate a multi dimensiona array with a series of these records. The number of questions may vary, so I can't make it a fixed size.

How would I declare this array, or would I be better off using an ArrayList (I can't seem to find any documentation on this) or should I use a List of Arrays?

:sign0085: Sterling
 

Jost aus Soest

Active Member
Licensed User
Longtime User
I would use a type like this:
B4X:
Type typeQuiz( _
  Question As String, Answers As Int, Solution As Int, _
  Answer1 As String, Answer2 As String, Answer3 As String, Answer4 As String)

With this type it's very easy to maintain the questions and answers, e. g. in an array:
B4X:
Dim Quiz(200) As typeQuiz

Quiz(0).Question = "How many legs has a dog?"
Quiz(0).Answers = 2
Quiz(0).Solution = 1
Quiz(0).Answer1 = "Most have 4."
Quiz(0).Answer2 = "A hot dog has no legs!"

Quiz(1).Question = "How many legs has a cat?"
Quiz(1).Answers = 3
Quiz(1).Solution = 2
Quiz(1).Answer1 = "3 or less."
Quiz(1).Answer2 = "Exactly 4."
Quiz(1).Answer3 = "5 or more."
 
Upvote 0

sterlingy

Active Member
Licensed User
Longtime User
Perfect!!

Jost,

This was a perfect solution.

Thanks so much!

:sign0188: Sterling
 
Upvote 0

Gary Hwang

New Member
Licensed User
Longtime User
Sorry,an error was triggered referring to the above method
my codes:
Type QueueType(Head As Byte , Tail As Byte , _
Size As Byte, Buff(100) As Byte)


Type MyType(CustomerNO As String, _
Buffer(100) As Byte, _
BufferIndex As Byte, _
SetFlag As Boolean, _
ZoneAlarm(12) As Boolean, _
PSW(8) As Byte, _
ControlFlag As Boolean, _
LastTSN As Byte , _
LastRSN As Byte , _
SerialNo As Byte , _
Retry As Int, _
RMCQue As QueueType)

Dim DL As MyType

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
If FirstTime = True Then
Activity.LoadLayout("Main")
txtIP.Text =MyIP
txtPort.Text =MyPort
InitializeQue(DL.RMCQue) ' ← this line call sub InitializeQue
LoadConst2Array
End If
End Sub

Sub InitializeQue(Q As QueueType)'初始化
Q.Head = 0 '← had error when run to this line
Q.Tail = 0
Q.Size = 100
End Sub
==========================================================
error description:
LogCat connected to: 0123456789ABCDEFG
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
** Activity (main) Create, isFirst = true **
Error occurred on line: 210 (main)
java.lang.NullPointerException: expected receiver of type b4a.example.main$_queuetype, but got null
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:680)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:308)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:121)
at b4a.example.main.afterFirstLayout(main.java:100)
at b4a.example.main.access$100(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:78)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5297)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
 
Upvote 0
Top