Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Dynamic alternative to 2D array

  • 31-05-2012 06:29PM
    #1
    Registered Users, Registered Users 2, Paid Member Posts: 8,000 ✭✭✭
    Something about sandwiches


    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, Registered Users 2 Posts: 2,062 ✭✭✭Colonel Panic


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


  • Registered Users, Registered Users 2 Posts: 356 ✭✭unknownlegend


    Looks like you want key value pairs?

    Dictionary datastructure should suffice?


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    What language? Seems to be java? -.-

    In c++ you could use vector of vectors.


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


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


  • Advertisement
  • Registered Users, Registered Users 2 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, Registered Users 2, Paid Member Posts: 8,000 ✭✭✭The_B_Man
    Something about sandwiches


    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, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


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


  • Registered Users, Registered Users 2 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, Registered Users 2, Paid Member Posts: 8,000 ✭✭✭The_B_Man
    Something about sandwiches


    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, Registered Users 2 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,525 Mod ✭✭✭✭dregin


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

    Diques should be used in python.


  • Registered Users, Registered Users 2 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