[java] Dołożenie następnych danych do instniejącej tablicy


#1

Witam,

mam pewne pytanie:

otóż mam taką tablicę:

public static String map[][] = {

        {"0sb0sa0ca0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0ca0sa0sb0ca0sb0sbC1"},

        {"0sb0sa0ca0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sa0ca0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sa0ca0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sa0ca0sb0sb0sbC1","0ca0ca0ca0ca0ca0caC0","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sa0ca0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sa0ca0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sa0ca0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sa0ca0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0sb0sa0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sa0ca0sb0ca0sbC1","0sb0sa0sb0sb0ca0sbC1","0sb0sa0sb0sb0ca0sbC1","0sb0sa0sb0sb0ca0sbC1","0sb0sa0sb0sb0ca0sbC1","0sb0sa0sb0sb0ca0sbC1","0sb0sa0sb0sb0ca0sbC1","0sb0sa0sb0sb0ca0sbC1","0sb0sa0sb0sb0ca0sbC1","0ca0sa0sb0sb0ca0sbC1"},

        {"nextfloor"},

        {"0sb0sb0ca0ca0sb0sbC1","0sb0sb0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0sb0sa0sb0ca0sb0sbC1","0ca0sa0sb0ca0sb0sbC1"},

        {"0sb0sb0ca0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sb0ca0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sb0ca0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sb0ca0sb0sb0sbC1","0ca0ca0ca0ca0ca0caC0","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sb0ca0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sb0ca0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sb0ca0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sb0ca0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0sb0sb0sb0sb0sb0sbC1","0ca0sa0sb0sb0sb0sbC1"},

        {"0sb0sb0ca0sb0ca0sbC1","0sb0sb0sb0sb0ca0sbC1","0sb0sb0sb0sb0ca0sbC1","0sb0sb0sb0sb0ca0sbC1","0sb0sb0sb0sb0ca0sbC1","0sb0sb0sb0sb0ca0sbC1","0sb0sb0sb0sb0ca0sbC1","0sb0sb0sb0sb0ca0sbC1","0sb0sb0sb0sb0ca0sbC1","0ca0sa0sb0sb0ca0sbC1"}

    };

(stworzoną ręcznie) i za pomocą tego kodu:

mapa[mapa.length+1][0] = "nextfloor";

chciałem dołożyć do tej tablicy jeszcze jedną kolumnę. Tylko, że jest błąd:

Podejrzewam, że w ten sposób nie da się dołożyć kolejnej kolumny. Więc czy jest jakiś sposób, żeby bez potrzeby tworzenia tablicy od nowa, dodać tą kolumnę?


(Jedras121) #2

Moim skromnym zdaniem można napisać metodę która będzie zwracać jako rezultat String[], która jako parametr przyjmie obecna tablice i jako drugi parametr nowy rozmiar. jedyne co trzeba zrobic to w metodzie przepisać dane z poprzedniej tablicy do nowej powiększonej

private String[] powieksz(String[] tab, int newX, int newY){...}

(Fiołek) #3

Od tego są kontenery(ArrayList czy Vector).


(Razi) #4

Albo kontenery, ale wtedy trochę trudniej jest z tablicami wielowymiarowymi (wtedy każdy element jednego wymiaru to kolejna tablica, każda "osobno"), albo od razu zarezerwuj większą.

Możesz istniejącą tablicę jednowymiarową przerzucić do ArrayList:

Array.asList(twoja_tablica)

Pamiętaj że tablica dwuwymiarowa to jednowymiarowa tablica tablic jednowymiarowych