Results 1 to 19 of 19
- 08-09-2011, 01:51 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
- 08-09-2011, 02:03 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
Investigate
Java Code:Object[] arr;
Last edited by pbrockway2; 08-09-2011 at 02:05 PM.
- 08-09-2011, 02:15 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
Well, I have to relate the size of a file and it's last access to it's type. So i thought if I did a 2D array that would have the size in one colomn, the last access in another and the type in the 3rd colomn..
The Object[] arr; you said, can you be more specific?
Thanks
- 08-09-2011, 02:35 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
More specifically, try declaring an object array of length 3 and then assigning an integral number, a date and something representing a filetype to the three elements.
-----
The better suggestion would be to define a class with the three quantities as instance variables. The data as a whole would then be modelled as a one dimensional array of instances of this class. The advantage of this approach is that you would obtain the access time for example from a "getter" which returns a specific type - a Date - and the compiler would catch mistakes like using the wrong index and thinking you were working with the file size or whatever.
- 08-09-2011, 07:26 PM #5
- 08-09-2011, 10:36 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 17
A List (Vector, ArrayList, etc) which is declared to hold instances of Object suffers from the same problem as an array of Object, namely that the type of the elements is not being specified anywhere. Whether to use a collection rather than an array is a question quite distinct from the one asked in this thread to which the answer remains: declare and use a class to represent file access information.
See, for instance, this discussion at stackoverflow.com.
- 08-10-2011, 05:56 AM #7
Member
- Join Date
- Aug 2011
- Posts
- 47
- Rep Power
- 0
After reading through my text, it sounds like you are looking for an Arraylist. I would read up on those.
Just remember that Arraylist stores objects. So, if you call the information pointed to by A[i], and it is an integer, you will need to typecast that data before you can use it in an equation. This is because you will be calling out an object from the array, not an integer. thankfully, one line of code will typecast it for you. Same deal if the data you are recalling from that index is a String, you will need to typecast it as a string before use.Last edited by Willriker; 08-10-2011 at 05:59 AM.
-
What? You can't "typecast" an int into a String as that simply won't compile. And besides if you want to represent int as an object, you'd use an Integer, not a String, and autoboxing should handle this for the coder. And besides, what pbrockway2 has said about not using arrays or array lists of different types still applies.
- 08-10-2011, 01:28 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
- 08-10-2011, 01:33 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
-
- 08-10-2011, 04:29 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
- 08-10-2011, 05:04 PM #13
Member
- Join Date
- Aug 2011
- Posts
- 47
- Rep Power
- 0
- 08-10-2011, 05:12 PM #14
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
-
You can't cast an Integer into an int. It just won't work. You would cast to (Integer), but all this casting has a bad smell to it.
Original poster: the true answer to this problem as we've been stating all along is not to muck with arrays or ArrayLists that hold different types. Again create a class to describe a "record" of data, and then create an array or ArrayList (or other collection) of objects of this class.
- 08-10-2011, 05:54 PM #16
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
- 08-10-2011, 06:49 PM #17
Member
- Join Date
- Aug 2011
- Posts
- 47
- Rep Power
- 0
7.3.3 ArrrayLists
The DynamicArrayOfInt class could be used in any situation where an array of int with no preset limit on the size is needed. However, if we want to store Shapes instead of ints, we would have to define a new class to do it. That class, probably named "DynamicArrayOfShape", would look exactly the same as the DynamicArrayOfInt class except that everywhere the type "int" appears, it would be replaced by the type "Shape". Similarly, we could define a DynamicArrayOfDouble class, a DynamicArrayOfPlayer class, and so on. But there is something a little silly about this, since all these classes are close to being identical. It would be nice to be able to write some kind of source code, once and for all, that could be used to generate any of these classes on demand, given the type of value that we want to store. This would be an example of generic programming. Some programming languages, including C++, have had support for generic programming for some time. With version 5.0, Java introduced true generic programming, but even before that it had something that was very similar: One can come close to generic programming in Java by working with data structures that contain elements of type Object. We will first consider the almost-generic programming that has been available in Java from the beginning, and then we will look at the change that was introduced in Java 5.0. A full discussion of generic programming will be given in Chapter 10.
In Java, every class is a subclass of the class named Object. This means that every object can be assigned to a variable of type Object. Any object can be put into an array of type Object[]. If we defined a DynamicArrayOfObject class, then we could store objects of any type. This is not true generic programming, and it doesn't apply to the primitive types such as int and double. But it does come close. In fact, there is no need for us to define a DynamicArrayOfObject class. Java already has a standard class named ArrayList that serves much the same purpose. The ArrayList class is in the package java.util, so if you want to use it in a program, you should put the directive "import java.util.ArrayList;" at the beginning of your source code file.
The ArrayList class differs from my DynamicArrayOfInt class in that an ArrayList object always has a definite size, and it is illegal to refer to a position in the ArrayList that lies outside its size. In this, an ArrayList is more like a regular array. However, the size of an ArrayList can be increased at will.
... skipped some text...
All this works very nicely. The only slight difficulty arises when you use the function players.get(i) to get the value stored at position i in the ArrayList. The return type of this function is Object. In this case the object that is returned by the function is actually of type Player. In order to do anything useful with the returned value, it's usually necessary to type-cast it to type Player:
Player plr = (Player)players.get(i);
For example, if the Player class includes an instance method makeMove() that is called to allow a player to make a move in the game, then the code for letting every player make a move is
for (int i = 0; i < players.size(); i++) {
Player plr = (Player)players.get(i);
plr.makeMove();
}
The two lines inside the for loop can be combined into a single line:
((Player)players.get(i)).makeMove();
This gets an item from the list, type-casts it, and then calls the makeMove() method on the resulting Player. The parentheses around "(Player)players.get(i)" are required because of Java's precedence rules. The parentheses force the type-cast to be performed before the makeMove() method is called.
I am very sorry if i am sounding like a bit of a pest, but i had intentions of producing a program using an array list so that i can work with items of multiple types. From what you are saying i might have to rethink my logic, my plan of action for the program.
I took this to mean that the statements players.get(i) and players.get(i+1) would both return objects. index[i] could refer to an object whose value would be, for example, "First Batter". To use index[i] elsewhere, you would have to type cast it into a string first, because it is currently an object. index[i+1] would reffer to an object whose value is, "32". to use index[i+1] elewhere, you would need to typecast it to int or double.
Am i correct in this interpretation, or am i just way off...Last edited by Willriker; 08-10-2011 at 06:52 PM.
- 08-10-2011, 07:02 PM #18
Pbrockway has given you the best advice in this thread. I'm surprised more people haven't told you not to do what you're asking how to do.
Get in the habit of using standard Java naming conventions!
- 08-10-2011, 08:27 PM #19
It is my understanding that casting is usually reserved for primitive types to convert to other primitive types:
double x;
int j = 12;
x = (double)j ;
If you want to convert and int(primitive) to a String(class) the Integer class has a static method:
Integer.toString( int i )
Conversely if you want to convert a String to an int then use the following:
Integer.parseInt( String s )If you aren't programming in Java, well that's just too bad.
I'd rather be using Ubuntu.
Similar Threads
-
Java Data Types ?!
By HearT.Hunt3r in forum New To JavaReplies: 4Last Post: 08-09-2011, 05:43 PM -
Creating an array from data in a text file
By Ryanas in forum New To JavaReplies: 4Last Post: 02-06-2011, 03:46 PM -
Comparing two data types in order to store in array
By gwithey in forum New To JavaReplies: 3Last Post: 05-01-2009, 11:27 AM -
Creating a Data Structure to Hold an Array and Distance Value
By rdoane in forum New To JavaReplies: 11Last Post: 04-01-2009, 04:43 AM -
Array of different data types?
By venkatteshb in forum New To JavaReplies: 1Last Post: 08-27-2008, 06:42 PM
Bookmarks