0

I have multiple JTables, and I want to create an array where it stores:

a) An index

b) A "pointer" to a certain JTable. I'm used to solving it this way from C++, but I'm new to Java.

Basically, instead of having an array of indeces for JTable1, one for JTable2, JTable3, and so on (since there will be a lot of these tables), I want to have just one big map linking an index to a JTable. I don't see how I could do this without pointers.

Bluejay
  • 341
  • 1
  • 10
  • 2
    Any variable referencing an Object in Java is essentially a pointer. – khelwood Jan 25 '17 at 09:16
  • 1
    Read this link:- http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value, it will clarify your doubt. – Amit Bhati Jan 25 '17 at 09:22
  • 1
    Your "something like a pointer" is called a reference. In Java, all variables of object type store a reference, rather than an actual object. So a variable of type `JTable` is exactly the "something like a pointer" that you need. – Dawood ibn Kareem Jan 25 '17 at 09:29

2 Answers2

2

You just need to have a map like below :-

Map<String, JTable> jTableMap=new HashMap<String, JTable>();

In above code, key is the name of JTable and value is corresponding JTable instance.

Java is by pass by value, so no need of any pointers.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
  • My question may have been a bit unclear, but this does work as a solution as well. What I meant was that I wanted an array of integers/JTables (with the integer not acting as a unique key). However, I can solve that by creating such a map, and then creating a 2d array of integers, where the second integer will be used to look up what array the first belongs to. – Bluejay Jan 25 '17 at 09:25
0

You do not need a pointer. Java uses references. Just store a reference.

In C++, you use the pointer because you don't want to hold a copy of the object. This is unnecessary in Java.

Axel
  • 13,939
  • 5
  • 50
  • 79