Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Dynamic alternative to 2D array

Options
  • 31-05-2012 6:29pm
    #1
    Registered Users Posts: 7,863 ✭✭✭


    If I have an array like so:

    String[][] myArray = [4096][4096];

    But may only use it for 10x10 rows, whats the best way to go about it?

    Can you do a vector of vectors? ArrayList of ArrayLists? Whats the most efficient option here?

    Thanks.


Comments

  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    A dynamic 1D array, a function to index into it and a function to calculate the required size.


  • Registered Users Posts: 375 ✭✭unknownlegend


    Looks like you want key value pairs?

    Dictionary datastructure should suffice?


  • Registered Users Posts: 58 ✭✭adigilani


    a hashmap would be good...
    read the documentations...
    The_B_Man wrote: »
    If I have an array like so:

    String[][] myArray = [4096][4096];

    But may only use it for 10x10 rows, whats the best way to go about it?

    Can you do a vector of vectors? ArrayList of ArrayLists? Whats the most efficient option here?

    Thanks.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    What language? Seems to be java? -.-

    In c++ you could use vector of vectors.


  • Registered Users Posts: 3,945 ✭✭✭Anima


    What are you actually trying to do? Kind of pointless making a suggestion without knowing.


  • Advertisement
  • Registered Users Posts: 586 ✭✭✭Aswerty


    Anima wrote: »
    What are you actually trying to do? Kind of pointless making a suggestion without knowing.

    My thoughts exactly, why are making an String array of size >16M in the first place?


  • Registered Users Posts: 7,863 ✭✭✭The_B_Man


    It was actually in a technical test I did the other day. It was the "bad code" section.

    They had the array of size [4096][]

    then in a loop they were going :

    while (true)
    {
    array[counter][] = new String[7];
    //do stuff
    counter++;
    }

    I think it was a String, but not 100%. I think it might have been some object of a class, now that I think of it. Point is though, declaring something that big is ridiculous!! I said they should have used a vector or something more dynamic, but then it struck me that I've never seen a 2D vector before, hence this question.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    There is no 2d vector because it isn't needed. A vector of vectors achieves the same result.


  • Registered Users Posts: 586 ✭✭✭Aswerty


    Well the first thing that stands out is you have an infinite loop assuming there is no break in //do stuff. You'd run into a overflow exception if the break doesn't exist. If there is a break a for loop would probably be a better fit.

    In this case it looks like a vector of string arrays could be the best choice. Though if the vector is long you want to be sure you don't have to access individual elements a lot since that requires you to iterate through the vector each time.


  • Registered Users Posts: 7,863 ✭✭✭The_B_Man


    ye ignore the infinite loop. That was me regurgitating the code from memory, and there was some other stuff inside that got around it.

    As I said at the end of my last post, I think it was a class object with 7 member variables. Think of it like a DB table so the array is like "array[row][column]" with an unknown number of rows, and 7 columns.

    The tech test is over now so I suppose it doesnt matter. It was just something I encountered and wanted to learn.

    So is it possible, in Java, to do a Vector<Vector<String>> myVector etc.... because I don't think a Vector<String> would do the same as a 2D array. Is there a such thing as a Vector[] ?


  • Advertisement
  • Registered Users Posts: 586 ✭✭✭Aswerty


    Vector<Vector<String>> myVector can be done.

    Not 100% on this (not used Java in a while) but I don't see any reason why a Vector<String>[] would not work since it is just an array of a generic object. While Vectors are extremely useful data structures they are not all that unique and function like any other object.


  • Closed Accounts Posts: 816 ✭✭✭Opinicus


    Yeah in a recent college project I had LinkedLists of LinkedLists and an array of LinkedLists also. Learnt a lot about objects and how they worked in java this way.


  • Moderators, Music Moderators Posts: 6,524 Mod ✭✭✭✭dregin


    Glanced through, but couldn't see what language you're talking about.

    Diques should be used in python.


  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    As Colonel Panic and Aswerty said, you don't need a 2d array/vector.

    If you can fix an array that doesn't need to grow, the length would be width * height. Otherwise just use a vector.

    Keep hold of your column width, then you could get the index for a (x, y) point (or (column, row) if you prefer) with something like:
    function indexOfPoint(x, y) {
        return x + (y * width);
    }
    


Advertisement