aspose file tools*
The moose likes Beginning Java and the fly likes help with 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 arraylists" Watch "help with arraylists" New topic
Author

help with arraylists

Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

I have to build an ArrayList of the class PixFiles and I'm not sure how to go about doing it. My professor looked over my code and claimed it was now correct after he changed it, but it won't work. I'm using the statement "ArrayList<PixFile> photos = new ArrayList<>();" and trying to populate it with photos of the PixFile class which include the name of the photo, the photographer's name and the size of the file. My question is this...when I get the information to add to the list, how do I save it in the array? He doesn't explain very well, he just expects you to either get it or not and I'm not. Snippet that is causing me trouble follows. Also, when I go to search the ArrayList, I'm not finding the information I entered. I'm assuming because I'm not adding it to the array correctly.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
That is a contraction ofRather than giving the name photo to the object, you create it inside the () for the add method. You can’t use that reference again, but another reference is safely hidden inside the List, where you can find it easily.

You might be able to run those two JOptionPane calls into one. I still sometimes use JOptionPane mysefl, but it is very old‑fashioned programming. You might be better off with a Scanner.
You should beware of anything taking seven parameters; it is error‑prone. There is something wrong about calculating storage cost outwith the FixFile class and then passing it in. You can calculate it whenever you require it with the methods in the Storage class. That is secondary information which can be calculated from the primary information of file size.
There is something else which has been missed: you ought not to declare the ArrayList as such. You should declare it as List<Pixfile>:-
Have you been through the Java Tutorials? That might be helpful.

You made the vague statement that it doesn’t work. Unless you tell us what is going wrong, we can’t help any more.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

You should declare it as List<Pixfile>:-

I do not believe this would work Ritchie. You would have to write it like We ought to provide the parametrized type on both sides if I am correct.


~ Mansukh
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

Mansukhdeep Thind wrote:
I do not believe this would work Ritchie. You would have to write it like We ought to provide the parametrized type on both sides if I am correct.


It a new feature so it works only with JAVA 7.. mostly they called as Diamond Brackets. I hope I am right ritchie..


OCPJP
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
They call it the diamond operator, and, as you say, it only works in Java7.
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Thanks, Campbell. So the code I have listed should work? Well, I don't seem to be able to access the arraylist to pull out the information. When I enter a new photo file called "mylife" then do a search for it, I get nothing in return, just the array out of bounds exception from the -1. All I want to do is save the file, (or up to 10 of them) to an arraylist, access them, and display them using JOptionPane. (We have to use JOP because that's the way the professor wants it.) Any suggestions?



Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
If you are using an ArrayList, why don’t you use its methods?
I do not think your method will work at all well. Have you ever got it to return a value greater than 0?
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

No actually, I haven't. That is where the problem comes in. I'm questioning if it's even going into the arraylist because no matter what I do I only get a -1 back. I've tried to read the tutorials, but they don't cover what I'm trying to do. Maybe I'm going about this the wrong way?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Marjorie Gyles wrote:No actually, I haven't. That is where the problem comes in. I'm questioning if it's even going into the arraylist because no matter what I do I only get a -1 back. I've tried to read the tutorials, but they don't cover what I'm trying to do. Maybe I'm going about this the wrong way?

On line 15 you have an ArrayList containing PixFile objects and you're checking to see if this List contains a String. This will always fail because a String is not a PixFile.
If you want to be able to access your PixFile objects using the file name you might want to look at using a Map instead of a List which has the file names as the keys and the PixFile instances as the values.

If you want (or have) to stick with Lists then you shouldn't be using the contains method. You need to get each instance from the List and then compare its file name with the file name you are looking for.

Joanne
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8216
    
  23

Marjorie Gyles wrote:I have to build an ArrayList...

Marjorie,

Please DontWriteLongLines (←click). It makes your thread (and your code) very hard to follow.
I'd correct yours but there are tons of them, so I suggest you break them up yourself (Use the 'Edit' button).

Thanks.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Winston Gutkowski wrote:
Marjorie Gyles wrote:I have to build an ArrayList...

Marjorie,

Please DontWriteLongLines (←click). It makes your thread (and your code) very hard to follow.
I'd correct yours but there are tons of them, so I suggest you break them up yourself (Use the 'Edit' button).

Thanks.

Winston


Winston,
What are you doing, looking at JavaRanch on a NetBook or something? There's nothing wrong with my lines. Only the comments wrap around and that was only after I submitted the replies. I suggest you recheck your screen size.
Marjorie
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Marjorie Gyles wrote: . . .
Winston,
What are you doing, looking at JavaRanch on a NetBook or something? . . .
There are many people who read this website on net books, palmtops, etc. Long lines are difficult to read because you have to scroll left‑and‑right. Some people post lines so long that they fall off the left and right margins of my 1920px wide screen.
Not only do long lines make it less likely that people will answer your post, but they are also discourteous to other users.

It is the poster’s responsibility to post something which others can read, not the reader’s responsibility to tailor their computer to what was posted.
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Campbell Ritchie wrote:

It is the poster’s responsibility to post something which others can read, not the reader’s responsibility to tailor their computer to what was posted.


Problem fixed in Eclipse.
Marjorie Gyles
Ranch Hand

Joined: Nov 03, 2012
Posts: 42

Campbell Ritchie wrote:
There is something else which has been missed: you ought not to declare the ArrayList as such. You should declare it as List<Pixfile>:-


Dumb question, but ... Why? I assume it matters, buy why does it matter? Something in the way Java is set up? (I don't like not understanding why I'm doing something.)


Campbell Ritchie wrote:
Have you been through the Java Tutorials? That might be helpful.

You made the vague statement that it doesn’t work. Unless you tell us what is going wrong, we can’t help any more.



I'm working through these pages as well as JavaNotes and so far, it has helped explain a little better. Thanks for the reminder. As for the vague statement, it's a case of where do I start with the error messages? LOL I guess the heart of the matter is that I don't understand how to get the information into the ArrayList when it is entered at the keyboard. I have been using the constructor with the seven parameters, but I'm not sure if it's being saved in the ArrayList or if it's just being saved in the variables. Now, I have to take this whole program that I don't have completed and add input from a delimited text file into the arraylist along with the above. I've got to figure this out. I've gotten bits and pieces, but they aren't fitting together very well. I don't know if I will ever understand Java. <just about to throw my hands up and give up>
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Marjorie Gyles wrote:
Campbell Ritchie wrote:
There is something else which has been missed: you ought not to declare the ArrayList as such. You should declare it as List<Pixfile>:-


Dumb question, but ... Why? I assume it matters, buy why does it matter? Something in the way Java is set up? (I don't like not understanding why I'm doing something.)


It will work either way, but it's better design practice to declare a variable as the least specific type that meets your needs. By declaring it as a List, you're saying, "All I care about is that it is a List--something to which I can add a bunch of items, remove items, iterate over what I've added, and the iteration order will be the same as the order I added them." In this particular case, you probably don't care whether that List is an ArrayList or LinkedList or some other implementation. Later, if you find a LinkedList works better, you only have to change the point where you instantiate it. All the List references that use it don't need to know or care about that implementation detail.

And if you don't actually care if the iteration order matches the insertion order, don't care about accessing by index, and don't need to iterate backward, and don't care whether duplicates are preserved, you could declare the variable as Collection.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help with arraylists