Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2005, 9:22 PM   #1
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 150
Rep Power: 4 Writlaus is on a distinguished road
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?
Writlaus is offline   Reply With Quote
Old Nov 30th, 2005, 10:19 PM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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
Mjordan2nd is offline   Reply With Quote
Old Nov 30th, 2005, 11:00 PM   #3
titaniumdecoy
Programming Guru
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 1,007
Rep Power: 5 titaniumdecoy will become famous soon enough
Send a message via AIM to titaniumdecoy
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?
titaniumdecoy is offline   Reply With Quote
Old Nov 30th, 2005, 11:03 PM   #4
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 150
Rep Power: 4 Writlaus is on a distinguished road
Thanks for the link, I do appreciate it.
Writlaus is offline   Reply With Quote
Old Nov 30th, 2005, 11:24 PM   #5
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 150
Rep Power: 4 Writlaus is on a distinguished road
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.
Writlaus is offline   Reply With Quote
Old Nov 30th, 2005, 11:39 PM   #6
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
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
groovicus is offline   Reply With Quote
Old Nov 30th, 2005, 11:57 PM   #7
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 321
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Quote:
Originally Posted by titaniumdecoy
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?

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
andro is offline   Reply With Quote
Old Dec 1st, 2005, 3:56 AM   #8
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 150
Rep Power: 4 Writlaus is on a distinguished road
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.
Writlaus is offline   Reply With Quote
Old Dec 1st, 2005, 8:14 AM   #9
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 5 groovicus is on a distinguished road
What kind of error is it giving you?
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Old Dec 1st, 2005, 8:28 AM   #10
JDStud6
Programmer
 
Join Date: Jan 2005
Location: Charleston, SC www.wareonearth.com
Posts: 57
Rep Power: 4 JDStud6 is on a distinguished road
Send a message via AIM to JDStud6
just go ahead and post all of your code. That way we could copy it and run it to see if we can help. It looks pretty good so far.

JD
JDStud6 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:01 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC