Newbie Question - ArrayLists and referencing it

T

Taria

Hello all,

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 {
public static void main(String[] args) {
ArrayList data = new ArrayList();
ArrayList [] table = new ArrayList[5];
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
failed ways that I've tried
//table=data.get(i);
//table(i)=data.get(i);
table[0] = data;
}
}

Essentially, I want to assign the first value of data to the first
element of table, the second value of data to table(2,0), and 3rd
element to table(3,0). The statement table[0] = data puts the whole
arrayList data to table(0) which is not what I want. How do you
reference the elements of table? I thought I understood the
referening until now. :x

Any help is appreciated.
-t
 
C

Chris ( Val )

Hello all,

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 {
public static void main(String[] args) {
ArrayList data = new ArrayList();

You are using an "unchecked" ArrayList here, try:
"ArrayList said:
ArrayList [] table = new ArrayList[5];
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
failed ways that I've tried
//table=data.get(i);
//table(i)=data.get(i);


Wrong syntax, and trying to assign to empty elements.
table[0] = data;
}
}

Essentially, I want to assign the first value of data to the first
element of table, the second value of data to table(2,0), and 3rd
element to table(3,0). The statement table[0] = data puts the whole
arrayList data to table(0) which is not what I want. How do you
reference the elements of table? I thought I understood the
referening until now. :x

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.
 
L

Lew

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>();"
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.

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=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.

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;
}
}

Essentially, I want to assign the first value of data to the first
element of table, the second value of data to table(2,0), and 3rd
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.

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.
 
D

Daniel Pitts

Chris said:
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 ) );
}
}
}
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.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.
 
T

Taria

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
 
L

Lew

Taria said:
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.

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 );
}
}
 
C

Chris ( Val )

Chris ( Val ) wrote:


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 ) );
}
}
}

Whoops, Chris, you're wrong :-(
Specifically> Arrays.fill( table, new ArrayList<Integer>() );

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.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.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top