• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with Arrays and ArrayLists

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use Arraylist as:
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, I tried that same thing but without the "new" :/ oh dear...

Well, I learned something today

Thank you.
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are Welcome
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>.
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because you're using raw ArrayList objects. Use generics:

 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a runtime exception. I asked for the actual compiler errors. These are two very different things.
 
David Pountney
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about this, but how do I see the compiler errors? I'm pretty new to Java

Thanks
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Campbell, he's using NetBeans, not Eclipse.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic