How to add more to the contents of a String array in java -
i'm trying find way add more filled array, user of program must select 1 of array example seat[0][1] , add name should added next seat they've chosen. there way of doing or there way of changing contents of part they've chosen name? i'm using 2d string array.here's code i've written far if please offer advice i'd grateful thanks.
{string [][] seat = new string[2][6]; seat[0][0] = "a.1"; seat[0][1] = "b.1"; seat[0][2] = "c.1"; seat[0][3] = "d.1"; seat[0][4] = "e.1"; seat[0][5] = "f.1"; seat[1][0] = "a.2"; seat[1][1] = "b.2"; seat[1][2] = "c.2"; seat[1][3] = "d.2"; seat[1][4] = "e.2"; seat[1][5] = "f.2"; //print out array here using for-loops system.out.println("please choose seat: "); chosenseat=keyboard.readstring(); system.out.println("please enter name booking: "); name=keyboard.readstring();}
arrays fixed-size so, should use collection
arraylist
:
list<list<string>> arr = new arraylist<>();
to add "row":
arr.add(new arraylist<>());
to add element row:
arr.get(0).add("..."); // add element first row arr.add(new arraylist<>()); // add row arr.get(1).add("..."); // add element second row
and element:
// ... arr.get(1).get(0); // first element of second row
note:
- remember indices in programming languages starts
0
. access first element have use index0
.
Comments
Post a Comment