![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
Java array functions?
I started in Java a bit ago, and I was wondering: are there any functions similar to PHP's array_push() and array_pop() methods? If not, is there another variable type I should use if the length of the array will change?
|
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
I think you would find ArrayLists to be useful for you.
http://java.sun.com/j2se/1.4.2/docs/...ArrayList.html
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Expert Programmer
|
You can't resize arrays in Java.
I was wondering, though, is there any way to store a basic data type such an int or double in a ListArray or any other type of array/list rathern than an Integer or Double object? |
|
|
|
|
|
#4 | |
|
Professional Programmer
|
Quote:
Make sure to import java.util.ArrayList. ArrayList<Integer> myList = new ArrayList<Integer>(); and the ArrayList api... http://java.sun.com/j2se/1.5.0/docs/...ArrayList.html |
|
|
|
|
|
|
#5 | |
|
Expert Programmer
|
Quote:
|
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
Thanks for the link, I do appreciate it.
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
This is no doubt a product of the fact that I'm new to Java, but...
How do I create an ArrayList? It says it can't find the class ArrayList when I try to initialize it like I would another object type. |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
Don't take this at all the wrong way, but have are you familiar with the Java api? You are possibly not declaring it properly, or do not have the proper library imported. Check here:
http://java.sun.com/j2se/1.5.0/docs/...ArrayList.html If you have a difficult time with it, just ask. They are a little difficult to understand at first (they were for me anyway). But it will tell you all you need to know. ![]()
__________________
HijackThis Team-SFDC |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 3
![]() |
I'm sure I'm making it painfully obvious I'm new to Java.
What I'm trying to use the ArrayList for is to keep track of dot objects in my applet. Dots are going to be created by the user with every click, which is why I needed to be able to add elements to it. My problem now is that when I use the ArrayList.get(int index) method, I can't use that to access the object's variables. Here are some parts of my code... public class MyPoint {
int x,y;
public MyPoint(int x1,int y1) {
x=x1;y=y1;
}
}ArrayList points=new ArrayList(); public boolean mouseDown(Event e, int x, int y) {
points.add(new MyPoint(x,y));
return false;
} public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0,0,windowWidth,windowHeight);
g.setColor(Color.blue);
for(int i=0;i<points.size();i++) {
g.fillOval(points.get(i).x,points.get(i).y,dotSize,dotSize);
}
}In that last bit, g.fillOval(points.get(i).x,points.get(i).y,dotSize,dotSize); cannot find x... *Sigh* I know I'm new to Java, and I'm sure it's something simple I have wrong. |
|
|
|
|
|
#10 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
What kind of error is it giving you?
__________________
HijackThis Team-SFDC |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|