Hello all,I've tried many different ways to assign a value to the position of mychoice in a data structure, ArrayList and would appreciate any hintshow 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 differentfailed ways that I've tried //table[i]=data.get(i); //table(i)=data.get(i); table[0] = data; } }Essentially, I want to assign the first value of data to the firstelement of table, the second value of data to table(2,0), and 3rdelement to table(3,0). The statement table[0] = data puts the wholearrayList data to table(0) which is not what I want. How do youreference the elements of table? I thought I understood thereferening until now. :xAny help is appreciated.-t
|
|
0
|
|
|
|
Reply
|
mchew02 (18)
|
11/10/2007 6:50:38 AM |
|
On Nov 10, 5:50 pm, Taria <mche...@hotmail.com> wrote:> 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<Integer> data = new ArrayList<Integer>();"> 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[i]=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' flagat the command line.--Chris
|
|
0
|
|
|
|
Reply
|
Chris
|
11/10/2007 11:57:39 AM
|
|
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 differentThis 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;>> }>> }>>>> 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.>> How do you reference the elements of table? I thought I understood the>> referening until now. :xtable[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.-- Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
11/10/2007 2:45:23 PM
|
|
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.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.-- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
|
|
0
|
|
|
|
Reply
|
Daniel
|
11/10/2007 6:47:34 PM
|
|
Hello all,Ohhh, thank you so much for addressing those 'unchecked' warnings. Ithink I'm up to 15 of them now and have been ignoring them. :)Thank you guys for excellent advice! I think I spent almost fourhours on that small segment, trying to assign values where I wantedthem to go (instead of them 'magically' appearing somewhereelse. :p) I learned a lot of things by experimenting though.Arrays and collections don't make a good mix eh? Ok, so I reorganizedmy code to exclude the array within an ArrayList. I thought I neededto do that to create the matrix structure I ultimately wanted. So nowI have an ArrayList of ArrayLists where table's definition not anarray. In short, I've done this (code doesn't include the fix toremove 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 eachrow of table. And I have a question about those curly braces. Idiscovered them last night and it appears they make whatever code inthe middle of them, local within the brackets. I don't remember everseeing them used in examples of other ppl's code anywhere before. ButI've found that without them, table assumes the values of only thelast set of assignments instead of 2 unique sets of assignment. Maybeit's better to make those parts into methods? Just curious about theusage of those.Thank you again for your help.-t
|
|
0
|
|
|
|
Reply
|
Taria
|
11/10/2007 8:21:58 PM
|
|
Taria wrote:> 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 ); }}-- Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
11/10/2007 8:57:28 PM
|
|
On Fri, 09 Nov 2007 22:50:38 -0800, Taria <mchew02@hotmail.com> wrote,
quoted or indirectly quoted someone who said :
>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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
11/11/2007 7:36:55 AM
|
|
On Nov 11, 5:47 am, Daniel Pitts<newsgroup.spamfil...@virtualinfinity.net> wrote:> 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>()" wouldcreate and add a *new unique copy* of an ArrayList intoeach 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 experimentingwith the version I posted, I forgot to revert back tothis 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
|
|
0
|
|
|
|
Reply
|
Chris
|
11/11/2007 1:00:38 PM
|
|
On Sat, 10 Nov 2007 12:21:58 -0800, Taria <mchew02@hotmail.com> wrote,quoted or indirectly quoted someone who said :>remove the unchecked warnings see http://mindprod.com/jgloss/generics.html for a simplified intro to how to generify your code.-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
11/12/2007 2:51:45 AM
|
|
|
8 Replies
80 Views
(page loaded in 0.1 seconds)
|