wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Help with Arrays and ArrayLists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help with Arrays and ArrayLists" Watch "Help with Arrays and ArrayLists" New topic
Author

Help with Arrays and ArrayLists

David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Hello,

In my program I'm trying to make an ArrayList that contains Class objects, I was using normal arrays to do this but realised I want to adjust the length of the array and not have a fixed length.

Here is my current array code;



How could I achieve the same thing, but instead of using arrays, use ArrayLists?

Hope this is enough information to explain whats going on.

Thanks
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

You can use Arraylist as:


Life is easy because we write the source code.....
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Wow, I tried that same thing but without the "new" :/ oh dear...

Well, I learned something today

Thank you.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

You are Welcome
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Sorry to be a pain but I'm having some problems with passing an arrayList to a method;

Model is another class;




With this, I get the error on the IDE that says "array required, but java.util.ArrayList found"

How would I be able to pass an arrayList containing the Model classes to the method?

Thanks again
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

You can't use [...] to get elements out of an ArrayList by index the way you would do it with an array. (You're trying that in line 9).

Use the get(...) method on the ArrayList instead:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Change the method signature to not accept a Model[] (or Model... with varargs) but a List<? extends Model> instead. Notice how I 1) use List instead of ArrayList, allowing you to switch to a LinkedList later on if needed, and 2) use a generic bound wildcard. This allows you to pass Lists of any sub class of Model as well. (Of course, this isn't necessary if you don't plan on doing that; then List<Model> will be good.)

Or you could overload the method: one method accepts a Mode[] or Model..., the other a List<? extends Model>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Thanks for the reply,

I tired that but it gives the following error;

"found Model, Model requires Object, Object4"

Any ideas about why I'm getting that? As far as I know, it's only Models that are in there.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

That's because you're using raw ArrayList objects. Use generics:

David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Thanks for the replys,

Still having problems, I tried the;



And changed "list" to arrayList like it is in my program, but still getting an error saying on the following line;



Saying;

"Cannot find symbol: arrayList"

Not understanding whats going on here, I'm gonna go continue doing some research into lists while I wait for a reply.

Thanks again.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Hi, can you show full code to us. I think it might because you had created ArrayList object in some method(i.e local to that method only).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

David, you renamed the List from arrayList to list. You need to change all uses of that variable as well.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Rob Spoor wrote:David, you renamed the List from arrayList to list. You need to change all uses of that variable as well.


Yes Rob is right. I think you had not change arraylist to list. You must have to write code as



instead of

David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Here is the full code, cut down so you can see the problem easier e.g.



Excuse the fact that you can't compile this, I don't really want to give you the full code as it's pretty long.

I managed to fix the error I was getting (It was the lack of a capital letter on "array(L)ist")

However now the code refuses to compile, I'm getting the error;

"Runtime Exception, Uncompilable source code" at GUI.java: 50

So it seems it doesn't like the creation of the list object.

Thanks for the help.

Also I changed the name "list" to "arrayList" on the;

private List<Model> arraylist = new ArrayList<Model>();

line, so shouldn't that mean I shouldn't have to rename anything?

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

You are clearly using an IDE, or you wouldn't be able to run code that doesn't even compile.

Check the error screen at the bottom. It tells you all the compiler errors you are getting. You should solve them one by one. Post an exact compiler error here if you're not sure how to solve it.
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
I looked at whats wrong with line 50 in my code (which is the "private List<Model> arrayList = new ArrayList<Model>();" line) but I can't see whats wrong with it.

The error message is this;

run:
java.lang.RuntimeException: Uncompilable source code
at GUI.<init>(GUI.java:50)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:785)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:662)
BUILD SUCCESSFUL (total time: 4 seconds)

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

"Uncompilable source code"? That's a strange error. Try cleaning and rebuilding the whole project from scratch.

What IDE are you using and what version of Java?
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
I did a clean and build but that changed nothing, the codes fairly large and contains a lot of code thats generated by Netbeans, so re doing it will take a lot of my tiime. So if we could avoid that, it would be much better.

If you want I could post the whole code here if you want to see it.

I'm using Netbeans 6.9.1 and Java 1.6

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

That's a runtime exception. I asked for the actual compiler errors. These are two very different things.
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
Sorry about this, but how do I see the compiler errors? I'm pretty new to Java

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Don't you have a display of errors on your IDE? If it's Eclipse™, try looking at the lines; if there is a red icon with a cross, hover your mouse pointer over it and see what appears. You may get suggestions for corrections. You should consider going back to programming from the command line/terminal.
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
I do have a display of errors of my IDE (Netbeans), I posted them above from the output section that gave me the errors when I tried to compile the program, is there anything else I'm missing?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
No, you provided the runtime exception from the IDE, probably Eclipse™. It doesn't tell us what the compiler errors are, as Rob Spoor has already told you.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Campbell Ritchie wrote:No, you provided the runtime exception from the IDE, probably Eclipse™.

Campbell, he's using NetBeans, not Eclipse.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Sorry, I hadnt' noticed that.

Doesn't NetBeans show the compiler errors when you try to compile the code. Aren't you supposed to click the compile button and then the run button afterwards? Then you should get a printout of errors.
David Pountney
Greenhorn

Joined: May 26, 2011
Posts: 27
I'm not sure how, But I undid everything I did today, tried running the program (Still nothing) so when I was looking to compile the program (Which I found out you can do, it's F9 to do it, but for another unkown reason it's greyed out and won't let me compile and I have no idea why) I closed Netbeans, opened it back up, and now it works perfectly fine :/

I'm starting to not like Netbeans anymore.

I'm gonna take it one step at a time adding the stuff you guys helped me with today, and let you know what happens.

EDIT: Okay, I uncommented this line;

private List<Model> arrayList = new ArrayList<Model>();

and the same error came back, straight away.

I'm going to try and get to compile the file, any ideas why the option is greyed out on the menu?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with Arrays and ArrayLists