Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D Matrix Equivalent of ArrayList  (Read 1313 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2013-03-26 02:21:12 »

So I thought that it might be a good idea to try to make what is stated in the title.  It has an initial size of 1x1 and can expand as much as you like. I figured it to be more convenient that having "String[][] matrix = new String[1][1];" and having to provide dimensions from the get go. Anyway, the code is obviously not perfect, feel free to use it and tell me if I've done anything wrong or hideously impractical.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
public class ExpandableMatrix<T> {
   private T[][] data;
   private boolean locked = false;
   @SuppressWarnings("unchecked")
   public ExpandableMatrix(){
      data = (T[][]) new Object[1][1];
   }
   public T get(int x,int y){
      try{
         return data[x][y];
      }catch(Exception e){
         return null;
      }
   }
   public void fill(T object){
      for(int x = 0; x < getWidth();x++){
         for(int y = 0; y < getHeight();y++){
            put(object, x,y);
         }
      }
   }
   public ExpandableMatrix<T> expand(int newWidth, int newHeight) {
      if(locked){
         new Exception("Matrix cannot be expanded, it is locked.").printStackTrace();
         return this;
      }
      T[][] newData = newMatrix(newWidth, newHeight);
     
      if(newWidth  < getWidth() || getHeight() > newHeight){

         try{
            this.data = getSection(0,0,newWidth, newHeight);
         }catch(Exception e){
            for(int x = 0; x < newWidth; x++){
               for(int y = 0; y < newHeight;y++){
                  newData[x][y] = get(x,y);
               }
            }
            this.data = newData;
         }
         return this;
      }

      for(int x = 0; x < data.length;x++){
         System.arraycopy(data[x],0, newData[x], 0, data[x].length);
      }
      this.data = newData;
      return this;
   }
   public void put(T object, int x, int y){
      if(x < 0 || y < 0)
         throw new IllegalArgumentException("X and Y must be positive");
      if(x > data.length  || y > data[0].length){
         expand(x,y);
      }
      data[x][y] = object;
   }
   @SuppressWarnings("unchecked")
   private T[][] newMatrix(int width, int height){
      return (T[][]) new Object[width][height];
   }
   
   public T[][] getSection(int x, int y, int width, int height){
      if(isValidPosition(x,y) || isValidPosition(x + width, y + height))
         throw new IllegalArgumentException("Unable to get section, section invalid");
     
      T[][] newData = newMatrix(width, height);
      for(int x1 = x;x1 < x + width; x1++){
         for(int y1 = y; y1 < y + height;y1++){
            newData[x1 - x][y1 - y] = get(x1,y1);
         }
      }
      return newData;
   }
   public boolean isValidPosition(int x, int y){
      return (x < 0 || x > data.length || y < 0 || y > data[0].length);
   }
   public void clear(){
      data = newMatrix(1,1);
   }
   public void lock(){
      locked = true;
   }
   public void remove(int x, int y){
      data[x][y] = null;
   }
   public T[][] asMatrix(){
      return data;
   }
   public int getWidth(){
      return data.length;
   }
   public int getHeight(){
      return data[0].length;
   }
}

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline cubus

Junior Member


Medals: 2



« Reply #1 - Posted 2013-03-26 02:50:33 »

there is a bug in put()

should be:
1  
2  
3  
4  
5  
6  
7  
8  
   public void put(T object, int x, int y){
      if(x < 0 || y < 0)
         throw new IllegalArgumentException("X and Y must be positive");
      if(x >= data.length  || y >= data[0].length){
         expand(x + 1,y + 1);
      }
      data[x][y] = object;
   }



and isValidPosition

should be:
1  
2  
3  
   public boolean isValidPosition(int x, int y){
      return (x >= 0 && x < data.length && y >= 0 && y < data[0].length);
   }

or the method should be called isInValidPosition and the > should be >= then (like in put())

for optimization you could store in the outer loops a reference to
1  
array[x]
and use this in the inner loops, but of course it depends on your matrix size and usage frequency if that makes sense and since you are using getters and setters in the loops i think it's not very time critical code Wink

further, in the expand() method i would copy the area of Math.min(newWidth, getWidth()) and Math.min(newHeight, getHeight()) and remove the try catch. this way you can handle the case when newWidth > getWidth but newHeight < getHeight() for example.

in getSection() you could replace the inner loop with a System.arraycopy()

and so on Wink

EDIT: damn how can i use the array index operator in normal text without getting this list thing?
Online HeroesGraveDev

JGO Kernel


Medals: 244
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #2 - Posted 2013-03-26 04:21:50 »

What's wrong with
List<List<T>>
?

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (24 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!