aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList test not working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList test not working?" Watch "ArrayList test not working?" New topic
Author

ArrayList test not working?

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

This produces some funny output (ArrayEntry@103c5 or something....). SOMETHING works here.... but I get garbled output when I println the ArrayList. I had it originally as an array.... then switched it because I realized I had no idea how many entries the proverbial end user was going to add.

My instinct is there's something wrong with the way I filled the ArrayList with arrays, or the way I declared or passed the variables..... the parseDouble lines were working well before the change.... and the loop was working fine.

Here's the code:





In all fairness and honesty, I don't want the answer..... just a pointer. Thanks!
Janeice


When you do things right, people won't be sure you've done anything at all.
tabiul mahmood
Greenhorn

Joined: Sep 17, 2009
Posts: 18
One obvious mistake that I can see is that there is no instance variable in you class ArrayEntry. So what is happening is that you are just calling the method buildEntry and the variable double[] entry is only scoped within the method. One thing that I can suggest is that you have a instance variable like

private double[] entry = new double[3];

Inside your buildEntry method you can initalize the double entry like

this.entry[0] = odo;
etc

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874




If you dont use the return double value from "buildEntry" method , you can declare return type as void and that makes the compiler happy too.



It sure prints the object as you have added object ("foo") to it. You need to iterate to get the values for each object.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Thanks for all your pointers! I'll work on this later this afternoon.....

One quick question.... if the return type is "void"..... doesn't that mean the method won't accept values either?

thanks,
Janeice
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


return type and method arguments are entirely independent. so you can have it as void and have arguments too.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

This produces some funny output (ArrayEntry@103c5 or something....). SOMETHING works here.... but I get garbled output when I println the ArrayList.


By default, the toString() method, which is used by the system out, calls the toString() method of the elements to create the list string to be printed.

Since you never overrode the toString() method in you ArrayEntry class, all it does is say that it is an ArrayEntry class with the indentity hash code -- or something like ArrayEntry@103c5.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

On line nine of your first listing, you probably should be doing this:

When you compile your class file, you probably get a message saying that using raw collections classes is deprecated. This should fix that.

For your second class, remember that primitive types and arrays of primitive types are different.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Alright..... here's the update.....

I think I really screwed up. **heavy sigh**

What I originally wanted was "an arraylist populated with arrays" .... in other words....

ArrayList[0] = x0[0] x0[1] x0[2]
ArrayList[1] = x1[0] x1[1] x1[2]
etc....

What I CREATED was "an arraylist populated with objects that look like arrays".....

ArrayList[n] = ArrayEntryn




It took me two days of looking at this and fighting with this output to figure out what I did wrong. Learning is painful sometimes. I think the confusion came from my presumption that "arrays ARE objects."

Is there any helping this, or should I start over?

Frustrated and confused,
Janeice

P.S. I did make the changes talked about in this thread. I'm not getting any error about depreciated anything during compiling.... but I AM getting a compiling warning about unsafe conditions.

John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

You can't access entries in an ArrayList like you can on an array. So doing this (assuming arrayList has been defined):

will not work. Take a look at the Javadoc for the List interface (which ArrayList implements) for a list (no pun intended) of methods you can use to access entries in the list.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

John de Michele wrote:Janeice:

You can't access entries in an ArrayList like you can on an array. So doing this (assuming arrayList has been defined):

will not work.


.... That was what I did last, in all lost hope of fixing this. Actually I tried:



Then tried to get w.entry[0], w.entry[1], etc to println..... it won't even compile.

.... that's when I thought I should come back here and ask for help before I gave up/ cried/ threw it away and started over.

I just have no idea what I'm doing. Like I said, I created what I THOUGHT I wanted.... and I'm pretty sure it's working on the inside (i.e. holding my values somewhere). But if there's no way for me to get the values OUT, I think I'm barking up the wrong tree. I still need to get the values out and do math on them. I have the code for the math and final output all set.... it's just a matter of figuring out how to get what I need out of the list of objects....

--Janeice
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

So, if you have an ArrayList of ArrayEntry objects:

Adding an ArrayEntry object is like this:

To get that ArrayEntry object back again, do this:


John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

I did use the




it doesn't compile. It's complaining about incompatible types.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

I changed:



to




..... compiled with no errors or warnings this time. It wouldn't take "List" .... I had to use "ArrayList"

Thanks for your help John!
... hopefully I can get my information out......
--Janeice
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11



YAY!!! I can get my data out!!! The rest is all logic and downhill from here!!!

Thanks so much!!!
Janeice
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Janeice:

You probably just need to import java.util.List. It's probably ok to leave it as-is for now, but in the future, you should program against the interface rather than the implementation. Your programs will be easier to maintain that way.

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

John de Michele wrote:Janeice:

you should program against the interface rather than the implementation. Your programs will be easier to maintain that way.



This sounds important..... what does it mean?

--Janeice
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Janeice DelVecchio wrote:One quick question.... if the return type is "void"..... doesn't that mean the method won't accept values either?
Campbell's classification of methods:
  • Those which require additional information to execute: they have parameters.
  • Those which don't require additional information to run: they don't have parameters.
  • Those which return a result: they have a return type.
  • Those which don't return a result: they are marked void.
  • Those which alter the state of anything in the object.
  • Those which don't alter the state of anything in the object.
  • Those which take information from anything in the object.
  • Those which don't take information from anything in the object.
  • You can make a grid of +- and see different kinds of methods with different combinations.

    There are a few special kinds: look at the return type of most of the methods of StringBuilder, which allow you to join several calls with .s.
    Look at )]Arrays#sort() which alters the parameter passed to it.

    Methods which take no information from the object and alter no information in the object are candidates for being made "static".

    The Arrays.sort() link should read: http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#sort(int[]) Copy-and-paste it if it won't open.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38851
        
      23
    John de Michele wrote: . . . program against the interface . . .
    He ahs already shown you

    List<Foo> fooList = new ArrayList<Foo>();

    You can replace ArrayList with LinkedList or similar in that initialisation, and change nothing else, and the program will continue to function.
    Janeice DelVecchio
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1665
        
      11

    Thanks Campbell for all the information.... :-)

    I actually used stringBuilder for the output of the report (I have a thread in the Swing category.... it's pretty funny/sad lol) ..... long story short is System.out.println always does the same thing, and I should listen to my "elders".... lol.

    I still don't know what interface I'm supposed to be programming for/against.... I understand that the list and arraylist can be interchanged, but still not sure what this interface is and what implementation he's referring to.

    Thanks again,
    Janeice
    John de Michele
    Rancher

    Joined: Mar 09, 2009
    Posts: 600
    I still don't know what interface I'm supposed to be programming for/against.... I understand that the list and arraylist can be interchanged, but still not sure what this interface is and what implementation he's referring to.


    Janeice:

    List is an interface. ArrayList is an implementation (if you look at the Javadoc for ArrayList, you'll see that it implements List). An interface is a 'contract' for behavior; classes that implement an interface must have implementations of the methods defined in the interface. What this means is that classes that implement interfaces have similar behavior (the details on how exactly the methods are implemented can be different). So, when I suggested that you do this:

    I was having you program against the List interface. Now let's say you find out that, for whatever reason, ArrayList just doesn't provide what you need for your program, and what you really want is a LinkedList (which also implements the List interface). So you can change the one line of your code, like this:

    and your program will work without needing any more changes.

    Here is the Javadoc for List, and for ArrayList. You'll notice that ArrayList has all of the methods specified in List.

    John.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38851
        
      23
    The interface you want in this instance is List and near the top of the page you get the names of commonly-used implementations (ie those implementations in the standard Java download, all 10 of them). You also get the methods which all classes implementing the List interface all have (25 of them: beware: some are labelled "optional"). You will notice at least one of those classes is abstract.

    If you look at the ArrayList documentation you find it
    Implements all optional list operations, and permits all elements, including null.
    So you can use any of the methods in List, and you can find them there, some of which are inherited unchanged from AbstractList or AbstractCollection. You will find two methods at least (trimToSize and ensureCapacity) which appear to be unique to ArrayList, but you can also find them in Vector.
    Now look at some of the other classes implementing List, of which LinkedList is probably the best-known. Note which methods are to be found there.

    So you can declare a list like thisand initialise it like thisor like thisBoth will work.

    Even more interesting, if you look at LinkedList, you find it implements several interfaces, so you can also declare your LinkedList as a QueueYou can fit ArrayDeque (pronounced "array-deck") or LinkedList into a Queue, or ArrayList and LinkedList into a List.
    Janeice DelVecchio
    Saloon Keeper

    Joined: Sep 14, 2009
    Posts: 1665
        
      11

    This is really interesting. Thanks!

    --Janeice
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38851
        
      23
    You're welcome
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: ArrayList test not working?