Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: accessing elements in a 2d ArrayList

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default accessing elements in a 2d ArrayList

    well, im almost done with my map editor except for one pretty big thing... i am having problems with drawing on the 2d ArrayList.

    i start out by declaring and instantiating the 2d arraylist.
    private ArrayList<ArrayList<Integer>> tileMap;
    public TileMap(int rows, int columns){
            for(int r=0;r<rows;r++){
        		tileMap.add(new ArrayList<Integer>());
        		for(int c=0;c<columns;c++)
        			tileMap.get(r).add(-1);
    }
    i added a getTile(row,col) and a setTile(row,col,tile) method to make the rest of the code look cleaner
    public void setTile(int row,int col,int number){
        	if(row>=0 && col>=0 && row<rows && col<columns)
        		tileMap.get(row).set(col,number);
       }
    public int getTile(int row,int col){
        	return tileMap.get(row).get(col);
       }


    then everything gets messed up when it draws. ive been trying very hard to figure it out but am having some difficulty.

    public void mouseDragged(MouseEvent e){
    	if(getTile(e.getY()/tileSet.tileHeight(),e.getX()/tileSet.tileWidth())!=tilePane.getCurrentTile())//if not already currentTile
    	            setTile(e.getY()/tileSet.tileHeight(),e.getX()/tileSet.tileWidth(),tilePane.getCurrentTile());//make it currentTile
        	}

    the x and y cords are flipped when drawing, but i feel that everything should be working as is.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: accessing elements in a 2d ArrayList

    maybe you are passing row and coloumn the wrong way around?
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: accessing elements in a 2d ArrayList

    thats what i thought but it seemed right, ill check again

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. [URGENT] - Problem accessing web sites with Java!
    By jguilhermemv in forum Java Networking
    Replies: 0
    Last Post: March 5th, 2010, 04:43 PM
  3. [SOLVED] Web portal accessing files on the user's system via the Java I/O stream?
    By rendy.dev in forum Web Frameworks
    Replies: 2
    Last Post: January 18th, 2010, 08:46 PM
  4. Method Adding elements to an array with certain restrictions
    By Newoor in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2009, 11:13 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 02:11 PM