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, 8:22 PM   #1
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 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, 9: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, 10:00 PM   #3
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
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, 10:57 PM   #4
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 310
Rep Power: 3 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, 2:38 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 855
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Quote:
Originally Posted by andro
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
Thanks for your help. Is it possible to do the equivalent of this but store primitive int data types, for example ArrayList<int>? I looked up "autoboxing" but I'm not sure that's what I want. I need something that will run very fast, which is why I don't want to have to keep converting from Integer to int and so forth.
titaniumdecoy is offline   Reply With Quote
Old Nov 30th, 2005, 10:03 PM   #6
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 Writlaus is on a distinguished road
Thanks for the link, I do appreciate it.
Writlaus is offline   Reply With Quote
Old Nov 30th, 2005, 10:24 PM   #7
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 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, 10:39 PM   #8
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 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 Dec 1st, 2005, 2:56 AM   #9
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 3 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, 7:14 AM   #10
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
What kind of error is it giving you?
__________________
HijackThis Team-SFDC
groovicus 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 9:25 PM.

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