> Hello all,
>
[quoted text clipped - 6 lines]
> public static void main(String[] args) {
> ArrayList data = new ArrayList();
You are using an "unchecked" ArrayList here, try:
"ArrayList<Integer> data = new ArrayList<Integer>();"
> ArrayList [] table = new ArrayList[5];
> data.add(1);
[quoted text clipped - 5 lines]
> //table[i]=data.get(i);
> //table(i)=data.get(i);
Wrong syntax, and trying to assign to empty elements.
> table[0] = data;
> }
[quoted text clipped - 8 lines]
>
> Any help is appreciated.
You are going to get a lot of warnings using "unchecked"
arrays, but you can reduce that a little bit as follows:
public class MyProg1
{
public static void main( String[] args )
{
ArrayList<Integer> data = new ArrayList<Integer>();
ArrayList table[] = new ArrayList[ 5 ];
Arrays.fill( table, new ArrayList<Integer>() );
data.add( 1 );
data.add( 3 );
data.add( 4 );
for( int i = 0; i < data.size(); i++ ) {
table[ i ].add( i, data.get( i ) );
}
}
}
To suppress the warnings completely, you will (AFAIK),
need to use an annotation, or use the '-nowarn' flag
at the command line.
--
Chris
Lew - 10 Nov 2007 14:45 GMT
Taria wrote:
>> I've tried many different ways to assign a value to the position of my
>> choice in a data structure, ArrayList and would appreciate any hints
>> how to do this. My program as follows (short version):
>>
>> import java.util.*;
>> public class MyProg1 {
Excellent name for the class. Seriously - many newbies would use a name part
like "Class" for something that we already know is a class. You gave a
correctly-capitalized name that has a meaning - it's your program number one,
hence "MyProg1". Perfect.
>> public static void main(String[] args) {
>> ArrayList data = new ArrayList();
Chris ( Val) pointed out:
> You are using an "unchecked" ArrayList here, try:
> "ArrayList<Integer> data = new ArrayList<Integer>();"
Taria wrote:
>> ArrayList [] table = new ArrayList[5];
You do not need an array of ArrayList. Your 'table' is not an ArrayList, but
a group of five ArrayLists.
>> data.add(1);
>> data.add(3);
>> data.add(4);
>> for ( int i = 0; i < 3; i++ ){
>> //table.add(data.get(i)); //these are 3 different
This failed because data.get(i) is an Integer (not an int), and table needs a
whole ArrayList, not just one Integer.
Also because add() is not a method of an array.
>> failed ways that I've tried
>> //table[i]=data.get(i);
This failed because data.get(i) is an Integer (not an int), and table needs a
whole ArrayList, not just one Integer.
>> //table(i)=data.get(i);
You use bracket notation '[]' not parentheses '()' to assign to an array element.
This failed because data.get(i) is an Integer (not an int), and table needs a
whole ArrayList, not just one Integer.
>> table[0] = data;
>> }
[quoted text clipped - 4 lines]
>> element to table(3,0). The statement table[0] = data puts the whole
>> arrayList data to table(0) which is not what I want.
You cannot do what you want because table is an array of ArrayList, not an
array of Integer.
>> How do you reference the elements of table? I thought I understood the
>> referening until now. :x
table[0] = data;
just like you did. Each element of table is a whole entire ArrayList
(possibly empty or null).
Oh, you want table to hold Integers instead of ArrayLists? Then you want:
List <Integer> data = new ArrayList <Integer> ();
data.add(1);
data.add(2);
data.add(3);
Integer [] table = new Integer [5];
for ( int ix = 0; ix < Math.min( data.size(), table.length ); ++ix)
{
table [ix] = data.get( ix );
}
FWIW, arrays and collections make uneasy bedfellows.

Signature
Lew
Daniel Pitts - 10 Nov 2007 18:47 GMT
> public class MyProg1
> {
[quoted text clipped - 14 lines]
> }
> }
Whoops, Chris, you're wrong :-(
Specifically
> Arrays.fill( table, new ArrayList<Integer>() );
This will put the *same* ArrayList into every slot in table.
While I think the OP is going about this problem the wrong way, the
solution he needed was:
table[i].add(data.get(i));
To the OP:
Try not to mix arrays and Collections. As a matter of fact, for the
most part its best to deal with Collections, and avoid arrays all together.

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Taria - 10 Nov 2007 20:21 GMT
Hello all,
Ohhh, thank you so much for addressing those 'unchecked' warnings. I
think I'm up to 15 of them now and have been ignoring them. :)
Thank you guys for excellent advice! I think I spent almost four
hours on that small segment, trying to assign values where I wanted
them to go (instead of them 'magically' appearing somewhere
else. :p) I learned a lot of things by experimenting though.
Arrays and collections don't make a good mix eh? Ok, so I reorganized
my code to exclude the array within an ArrayList. I thought I needed
to do that to create the matrix structure I ultimately wanted. So now
I have an ArrayList of ArrayLists where table's definition not an
array. In short, I've done this (code doesn't include the fix to
remove the unchecked warnings):
...
ArrayList table = new ArrayList();
{ArrayList data = new ArrayList();
data.add(1);
data.add(3);
data.add(2);
table.add(data);
}
{ArrayList data = new ArrayList();
data.add(11);
data.add(13);
data.add(12);
table.add(data);
}
....
This is what worked for me, I assigned a list of data items to each
row of table. And I have a question about those curly braces. I
discovered them last night and it appears they make whatever code in
the middle of them, local within the brackets. I don't remember ever
seeing them used in examples of other ppl's code anywhere before. But
I've found that without them, table assumes the values of only the
last set of assignments instead of 2 unique sets of assignment. Maybe
it's better to make those parts into methods? Just curious about the
usage of those.
Thank you again for your help.
-t
Lew - 10 Nov 2007 20:57 GMT
> Hello all,
>
[quoted text clipped - 38 lines]
> it's better to make those parts into methods? Just curious about the
> usage of those.
Good questions.
The curly braces introduce a "block" of code with "local scope". Within each
block you *redeclared* the variable 'data'. Without the inner sets of curly
braces, the scope of the first declaration would have overlapped the second,
causing a conflict.
However, if you simply *re-use* the variable that problem goes away:
public class Matriculate
{
List< List <Integer> > table
= new ArrayList< ArrayList <Integer> > ();
public Matriculate()
{
// the table will now contain zero rows
assert table.size() == 0;
// here row is declared the one and only time
// and initialized for the first of more than one time
List <Integer> row = new ArrayList <Integer> ();
row.add( 1 );
row.add( 2 );
row.add( 3 );
row.add( 5 );
table.add( row );
// the table will now contain one row
assert table.size() == 1;
row = new ArrayList <Integer> ();
// notice - re-used, not re-declared
// the variable 'row' now points to a whole
// new ArrayList
row.add( 1 );
row.add( 2 );
row.add( 4 );
row.add( 8 );
row.add( 16 );
table.add( row );
// the table will now contain two rows
assert table.size() == 2;
}
// now the variable 'row' is out of scope
// the closing curly brace killed it
public List< List <Integer>> getTable()
{
return Collections.unmodifiableList( table );
}
}

Signature
Lew
Roedy Green - 12 Nov 2007 02:51 GMT
>remove the unchecked warnings
see http://mindprod.com/jgloss/generics.html
for a simplified intro to how to generify your code.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Chris ( Val ) - 11 Nov 2007 13:00 GMT
On Nov 11, 5:47 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.net> wrote:
> > public class MyProg1
> > {
[quoted text clipped - 19 lines]
>
> This will put the *same* ArrayList into every slot in table.
Wow :-)
My apologies to the OP.
I guess I assumed that "new ArrayList<Integer>()" would
create and add a *new unique copy* of an ArrayList into
each slot of the array.
Seems I have a bit more to learn about Java references.
Thank you for the correction.
> While I think the OP is going about this problem the wrong way, the
> solution he needed was:
>
> table[i].add(data.get(i));
Yes, I had that originally, but whilst experimenting
with the version I posted, I forgot to revert back to
this one.
> To the OP:
> Try not to mix arrays and Collections. As a matter of fact, for the
> most part its best to deal with Collections, and avoid arrays all together.
Good advice.
--
Chris
>I've tried many different ways to assign a value to the position of my
>choice in a data structure, ArrayList and would appreciate any hints
>how to do this. My program as follows (short version):
Are you confusing arrays and ArrayLists.
See http://mindprod.com/jgloss/array.html
http://mindprod.com/jgloss/arraylist.html
reviewing the basics of each should get help you solve this and many
similar problems.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com