Newbie Question: ArrayLists and Referencing

T

Taria

Hello all,

Yes, it's me again and ArrayLists. <grins> I'm having trouble
referencing the individual element of an ArrayList within an ArrayList
so that I can initialize another ArrayList in a single ArrayList way.

Let ArrayList A = a series of Arraylists where
0th element = {1,3,4,2}
1st element = {4,4,2}
2nd element ={1,7,2}
etc

What I hope B arraylist will look like:
B is a simple ArrayList
0th element = 1
1st element = 3
2nd element = 4 and so on.

So to reference the number 1 in the the A arraylist would be:
System.out.println ("ArrayList A(0,0) = " +
(ArrayList).get(0).get(0));

and to reference the number 1 in the B arraylist would be:
System.out.println ("ArrayList B(0) = " + Arraylist.get(0));

Now, what I want to do is, give ArrayList B the values of the 0th row
Arraylist 1,3,4,2 where ArrayList B (0)=1, ArrayList(1) =3 and so on.

I have tried this:
for ( int i = 0; i < A.get(0).size(); i++ ) {
node.add((ArrayList) data.get(0).get(i));
}

At the node.add I'm getting a 'symbol not found' error. So is this
because the add method doesn't support the reference type I'm passing?

It's late and I'm too tired to create a short SSCE to show this in a
program. I hope this is enough to convey what I'm trying to do and
the error I'm receiving. I'll try to create a short version between
work and school tomorrow.

As always, your comments and hints are appreciated.

-t (feeling a little perturbed at ArrayLists)
 
C

Chris ( Val )

Hello all,

Yes, it's me again and ArrayLists. <grins> I'm having trouble
referencing the individual element of an ArrayList within an ArrayList
so that I can initialize another ArrayList in a single ArrayList way.

What does "single ArrayList way" mean?
Let ArrayList A = a series of Arraylists where
0th element = {1,3,4,2}
1st element = {4,4,2}
2nd element ={1,7,2}
etc
Ok.

What I hope B arraylist will look like:
B is a simple ArrayList
0th element = 1
1st element = 3
2nd element = 4 and so on.
Ok.

So to reference the number 1 in the the A arraylist would be:
System.out.println ("ArrayList A(0,0) = " +
(ArrayList).get(0).get(0));

A.get( 0 ).get( 0 );
and to reference the number 1 in the B arraylist would be:
System.out.println ("ArrayList B(0) = " + Arraylist.get(0));

B.get( 0 );
Now, what I want to do is, give ArrayList B the values of the 0th row
Arraylist 1,3,4,2 where ArrayList B (0)=1, ArrayList(1) =3 and so on.

I have tried this:
for ( int i = 0; i < A.get(0).size(); i++ ) {
node.add((ArrayList) data.get(0).get(i));

}

Why the attempt to cast here?
Are you still getting warnings?
At the node.add I'm getting a 'symbol not found' error. So is this
because the add method doesn't support the reference type I'm passing?

You haven't shown what "node" is, or what the 'symbol not
found' error' was referring to, but it looks like "node"
doesn't support an "add()" method.
It's late and I'm too tired to create a short SSCE

ITYM: "SSCCE"
to show this in a
program. I hope this is enough to convey what I'm trying to do and
the error I'm receiving. I'll try to create a short version between
work and school tomorrow.

As always, your comments and hints are appreciated.

If "A" represents an "List<ArrayList<Integer>>", and "B" represents
a list of "ArrayList<Integer>", then the following should be all you
need to achieve what you want:

for( int index = 0; index < A.get( 0 ).size(); ++index ) {
B.add( A.get( 0 ).get( index ) );
}
 
D

derek

Hello all,
Yes, it's me again and ArrayLists. <grins> I'm having trouble
referencing the individual element of an ArrayList within an ArrayList
so that I can initialize another ArrayList in a single ArrayList way.
Let ArrayList A = a series of Arraylists where
0th element = {1,3,4,2}
1st element = {4,4,2}
2nd element ={1,7,2}
etc
What I hope B arraylist will look like:
B is a simple ArrayList
0th element = 1
1st element = 3
2nd element = 4 and so on.
So to reference the number 1 in the the A arraylist would be:
System.out.println ("ArrayList A(0,0) = " +
(ArrayList).get(0).get(0));

Actually it would be:

((ArrayList)A.get(0)).get(0)

You would need to cast the result of the get() method, unless you are using generics and have defined the ArrayList like this:

ArrayList said:
and to reference the number 1 in the B arraylist would be:
System.out.println ("ArrayList B(0) = " + Arraylist.get(0));
Now, what I want to do is, give ArrayList B the values of the 0th row
Arraylist 1,3,4,2 where ArrayList B (0)=1, ArrayList(1) =3 and so on.
I have tried this:
for ( int i = 0; i < A.get(0).size(); i++ ) {

Do you have A defined as
ArrayList A;
or
node.add((ArrayList) data.get(0).get(i));

Do you have node defined as an ArrayList?

ArrayList node;

Otherwise you will get the "symbol not found" error.

Check to make sure you have declared the variable.
 
L

Lew

Taria said:
So to reference the number 1 in the the A arraylist would be:
System.out.println ("ArrayList A(0,0) = " +
(ArrayList).get(0).get(0));

Not legal Java. (ArrayList) is a cast, but you apply a dot to it instead of
giving it a variable that points to an ArrayList. You need to do what Chris (
Val ) said:
A.get( 0 ).get( 0 );

Except that "A" as a variable name violates the Sun Java coding conventions.
A variable that is not a constant should have a lower-case first letter in its
name.
and to reference the number 1 in the B arraylist would be:
System.out.println ("ArrayList B(0) = " + Arraylist.get(0));

"Arraylist" isn't spelled correctly. get() is not a static method - you need
an ArrayList instance.
Now, what I want to do is, give ArrayList B the values of the 0th row
Arraylist 1,3,4,2 where ArrayList B (0)=1, ArrayList(1) =3 and so on.

List <List <Foo> > fooMatrix = new ArrayList< ArrayList <Foo>> ();
fillTheMatrix( fooMatrix );
List said:
I have tried this:
for ( int i = 0; i < A.get(0).size(); i++ ) {
node.add((ArrayList) data.get(0).get(i));

What is 'data'? By the syntax, it's a primitive, so the cast to ArrayList
will fail.
}

At the node.add I'm getting a 'symbol not found' error. So is this
because the add method doesn't support the reference type I'm passing?

You need to post a *complete* (short) example and the *complete* text (copied
and pasted) of your error messages. Paraphrases are a Bad Idea.

Read and take to heart:
<http://www.physci.org/codes/sscce.html>
 
L

Lew

derek said:
... unless you are using generics and have defined the ArrayList like this:

ArrayList<ArrayList> A = new ArrayList<ArrayList>();

It's a bad idea to mix generic and non-generic declarations like this. "A" as
a variable name violates the Java coding conventions and is far too terse to
be useful.

List < List <?> > matrix = new ArrayList < ArrayList <?> > ();
 
L

Lew

What is 'data'? By the syntax, it's a primitive, so the cast to
ArrayList will fail.

Oops. I meant that by the syntax it retrieves a single element of a single
row of the matrix List, so the cast of that single element to ArrayList will fail.
 
T

Taria

G'morning!

I have a few minutes before I have to get ready for school so I
conjured up a SSCCE (thank you for the correction of the acroynon,
btw) and it's as follows:

import java.util.*;
public class MyProg3 {
public static void main(String[] args) {
List table = new ArrayList ();
ArrayList <Integer> data = new ArrayList <Integer>();
data.add(1);
data.add(3);
data.add(4);
data.add(2);
table.add(data);

ArrayList <Integer> node = new ArrayList <Integer> ();

//trying to assign the value at table (0,0) to node(0)
node.add(table.get(0).get(0));
}
}

I encounter a compiling error at this point (where I was stuck last
night.)

I need to be able to extract values from the row of table (the multi
dimensioned ArrayList) into a simple dimensioned ArrayList (node). I
realize that I could simply equate data to the node variable BUT
further in my code, I will not have the luxery of simply using a
premade variable and will have to rely on the values of table to
initialize the variable node.

(Note: I tried to put the word <Integer> in my definition of table
but I encountered an error that it couldn't find the symbol at the
statement of "table.add...". Rather than wrestling with the compiler
on that note, I am living with the warning message.)

I apologize, Lew in particular, for trying to paraphrase my problem
without the exact syntax. I should have labeled my code as pseudo to
convey my concern.

I found a workaround when I first woke up today. (I was dreaming in
Java code! There is no rest for the weary, I tell ya.)

In place of the node assignment I have created a method (a one liner)
and it's calling statement as follows:

node = fillNode((ArrayList)table.get(0),0);

public static ArrayList fillNode (ArrayList <Integer> data, int
index);
ArrayList <Integer> node = new Arraylist<Integer>(data);
return node;

And by jove this works. I was going to use index to tell the method
which row I needed but found I really don't need this variable. Also,
I would prefer to not create a method to initalize my variable, node
but I do not see how. I had to use the cast on table while calling
the method because the compiler complained about it not being the
right type. (I wish I could look back at my compiler messages to tell
you specifically what it said but I have run out of time and I cannot
be late to class. :x)

I will review all of your posts with more scrutiny when school is over
in 9 hours...thank you again!

-t
 
T

Taria

G'morning!

I apologize for paraphasing my concern and bad naming conventions. I
should have labelled my code as psuedo. In any case, I created the
SSCCE (thank you for the correction) with the compiler error.

import java.util.*;
public class MyProg3 {
public static void main(String[] args) {
List table = new ArrayList ();
ArrayList <Integer> data = new ArrayList <Integer>();
data.add(1);
data.add(3);
data.add(4);
data.add(2);
table.add(data);

ArrayList <Integer> node = new ArrayList <Integer> ();

node.add(table.get(0).get(0));
}
}

At the 'node.add' statement the symbol cannot be found error happens
there. Also, if I were to insert the keyword <Integer> in my table
definition, I encounter another error and so I removed it because I
can live with the warning, for now anyway.

I realize I could probably equate 'data' to 'node' right off the bat,
but let's assume I needed to assign the row values of table to node
where node is a single dimensioned ArrayList further down my program
where the value is derived in a series of data manipulation. I want
to be able to extract the value of table(0,0) to node(0), table (0,1)
to node(1), etc. (until the data in row is exhausted)

I found a workaround when i first woke up this morning. (I was
dreaming in Java last night! No rest for the weary, I tell ya.)

The calling statement and the one line method as follows:

node = fillNode((ArrayList)table.get(0));

public static ArrayList fillNode(ArrayList <Integer> data){
ArrayList <Integer> node = new ArrayList <Integer>(data);
return node;

In place of the single assignment of the original program, this
works. I'd rather not have to use a method to do this job but for mpw
that's the solution I found that does what I want it to do. The cast
was necessary within the calling statement else it would not compile.

I have run out of time and MUST run off to class. I cannot be late!

I will review all your advice with greater scrutiny after class (9
hours from now) and thank you all again!

-t
 
P

Patricia Shanahan

Taria said:
G'morning!

I apologize for paraphasing my concern and bad naming conventions. I
should have labelled my code as psuedo. In any case, I created the
SSCCE (thank you for the correction) with the compiler error.

import java.util.*;
public class MyProg3 {
public static void main(String[] args) {
List table = new ArrayList ();
ArrayList <Integer> data = new ArrayList <Integer>();
data.add(1);
data.add(3);
data.add(4);
data.add(2);
table.add(data);

ArrayList <Integer> node = new ArrayList <Integer> ();

node.add(table.get(0).get(0));
}
}

import java.util.*;

public class MyProg3 {
public static void main(String[] args) {
List<List<Integer>> table = new ArrayList<List<Integer>>();
List<Integer> data = new ArrayList<Integer>();
data.add(1);
data.add(3);
data.add(4);
data.add(2);
table.add(data);

ArrayList<Integer> node = new ArrayList<Integer>();

node.add(table.get(0).get(0));
System.out.println(node);
}
}

The final println is just so that the program outputs something to check
that it did the right thing.

You need to think through the meaning of each data structure, and the
type will follow. "table" is supposed to refer to a list of lists of
integers, hence <List<List<Integer>>.

Patricia
 
T

Taria

Apology ahead of time, I posted my orig message, couldn't find it,
rewrote it and now i see my orignal message again. bleah, I'm so
embarrassed.

ok well, I'm sorry.

-t (the embarrrassed late school person)
 
C

Curt Welch

Patricia Shanahan said:
Taria said:
G'morning!

I apologize for paraphasing my concern and bad naming conventions. I
should have labelled my code as psuedo. In any case, I created the
SSCCE (thank you for the correction) with the compiler error.

import java.util.*;
public class MyProg3 {
public static void main(String[] args) {
List table = new ArrayList ();
ArrayList <Integer> data = new ArrayList <Integer>();
data.add(1);
data.add(3);
data.add(4);
data.add(2);
table.add(data);

ArrayList <Integer> node = new ArrayList <Integer> ();

node.add(table.get(0).get(0));

Taria, at this point, what does the complier think:

table.get(0)

will return?

It will return the ArrayList you called data. But the compiler will not
look at your code and figure that out. You must tell the compiler what it
will return when you define table.

When you defined table, you just defined it as ArrayList. You didn't
specify what type of objects you were going to put into it. As such, the
compiler has not idea what might be in the ArrayList, or what type of
object is going to come out. So it simply assumes the type is Object (the
default if you don't tell it what you are going to put in it).

You then tried to do a .get(0) on what came out of the table. The compiler
will not let you do that because get() is not a method that is known to
work on a object of type Object.

You can make you code work one of two ways.

You can use Generics, and tell the compiler what type of objects will be in
the ArrayList. That is the correct modern way to do it. But it's
confusing if you don't understand generics yet.

Patricia shows you how to do that here:
import java.util.*;

public class MyProg3 {
public static void main(String[] args) {
List<List<Integer>> table = new ArrayList<List<Integer>>();
List<Integer> data = new ArrayList<Integer>();
data.add(1);
data.add(3);
data.add(4);
data.add(2);
table.add(data);

ArrayList<Integer> node = new ArrayList<Integer>();

node.add(table.get(0).get(0));
System.out.println(node);
}
}

Once you define all the ArrayLists correctly, the node.add() works fine as
you wrote it.

That's because the compiler knows that get() is a method that you can use
on a List, so "table.get(0)" compiles fine. And the compiler also knows
that table is a List which holds other Lists which holds Integer objects.
So it knows that table.get(0) will return a list. So it knows that
table.get(0).get(0) is always valid, and that it will return an Integer
object.

And, when you try do do node.add() with an Integer, that also fits how node
was defined, (a list of Integer objects).

Everything works fine and the compiler won't let you make the mistake.

But the other option, (the old and bad way) is to not use Generics (only
because you don't really understand them yet), and simply tell the compiler
what to expect by casting everything as you use it.

Here's how to write your code without using generics:

import java.util.*;

public class MyProg3
{
public static void main(String[] args) {
ArrayList table = new ArrayList();
ArrayList data = new ArrayList();
data.add(1);
data.add(3);
data.add(4);
data.add(2);
table.add(data);

ArrayList node = new ArrayList();

//trying to assign the value at table (0,0) to node(0)
node.add(((ArrayList)table.get(0)).get(0));
}
}


All you have to do, is tell the computer that the table.get(0) is going to
be returning an ArrayList object. And you do that, by adding the cast and
the parens as I did above.

This is dangerous, because if you make a mistake in your code, and it turns
out that something else in your table at position 0, then your program will
blow up at run time with cast exception. Using Generics, (which is more
complex) allows the compiler to check at compile time that you never put
anything except ArrayLists, into your table.

Without generics, you can put anything you want into ArrayLists, but you
have to tell the compiler with a cast, what it is when you take it out,
before you can use it.
 
T

Taria

PLEASE. we have asked you before. Follow the coding conventions. Your
code is almost impossible to understand because:
1. it flagrantly violates the conventions
2. you mix pseudocode with real code.
3. you just post fragments, not your entire program.

seehttp://mindprod.com/jgloss/codingconventions.htmlhttp://mindprod.com/jgloss/sscce.html

Hello all,

Sorry Roedy, I'll stop from posting fragments unless it's clear. Also
that was a typo in which I gave as a fragment. I'll be sure not to be
tired when I post here, i'm prone to many mistakes.

And thank you Curt and Patricia for your patient explanations,
especially the part about the generics. I just got home from a long
day of school and about to dive in Java until work for a few hours.

-t
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top