Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2007

Tip: Looking for answers? Try searching our database.

assignining an element from an ArrayList to a variable

Thread view: 
Art Cummings - 08 Dec 2007 21:51 GMT
Does anyone know how to assign an element from an ArrayList to a variable?
Specifically, I know that I can print an item using
System.out.println(nameList); but how do I assign the ArrayList element to a
string variable?

Thanks

Arrt

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.util.ArrayList; // to use array list
import java.util.Scanner; // Needed for the Scanner class
import java.io.*;         // Needed for the File class

/**
  This program demonstrates the ArrayList toString method.
*/

public class ArrayListDemo2
{
  public static void main(String[] args)
  {
     // Create a vector to hold some names.
     ArrayList nameList = new ArrayList();
 String name;

     // Add some names to the ArrayList.
     nameList.add("James");
     nameList.add("Catherine");
     nameList.add("Bill");

     // Now display the items in nameList.
     System.out.println(nameList);

  }
}
Lew - 08 Dec 2007 22:05 GMT
> Does anyone know how to assign an element from an ArrayList to a variable?
> Specifically, I know that I can print an item using
> System.out.println(nameList); but how do I assign the ArrayList element to a
> string variable?

You keep saying the tutorial is "too much to digest", then asking these
really, really elementary questions.  How can you even understand the answers
if you won't do the minimum to understand the very least basics of the
language?  You're not only wasting our time, you're wasting your time.

> import javax.swing.*;
> import java.awt.*;
[quoted text clipped - 14 lines]
>       // Create a vector to hold some names.
>       ArrayList nameList = new ArrayList();
// Often not a good idea to include implementation in a variable name.

  /*
>   String name; unused so delete it */
>
[quoted text clipped - 4 lines]
>
>       // Now display the items in nameList.

        for ( String name : nameList )
        {
>       System.out.println( name );
        }
>    }
> }

I promise you, the basic tutorials
<http://java.sun.com/docs/books/tutorial/java/index.html>
and
<http://java.sun.com/docs/books/tutorial/essential/index.html>
will not take all that long to work through - certainly *far* less than asking
questions to the newsgroup and being befuddled by the answers.

You want us to do all your work for you, when you won't even do the minimum to
understand *any* aspect of the Java language?

Signature

Lew

Lew - 08 Dec 2007 22:06 GMT
>> Does anyone know how to assign an element from an ArrayList to a
>> variable? Specifically, I know that I can print an item using
[quoted text clipped - 25 lines]
>>       // Create a vector to hold some names.
>>       ArrayList nameList = new ArrayList();

Oops - I forgot to correct this to:
         ArrayList <String> nameList = new ArrayList <String> ();
>   /*
>>   String name; unused so delete it */
[quoted text clipped - 22 lines]
> You want us to do all your work for you, when you won't even do the
> minimum to understand *any* aspect of the Java language?

Signature

Lew

Art Cummings - 08 Dec 2007 22:17 GMT
> I promise you, the basic tutorials
> <http://java.sun.com/docs/books/tutorial/java/index.html>
[quoted text clipped - 5 lines]
> You want us to do all your work for you, when you won't even do the
> minimum to understand *any* aspect of the Java language?

Lew, thanks, but dig, I've been taking this class for 8 weeks, i've got one
of the highest grades in the class.  I've been working on this particular
project for almost 70plus hours,  i've combed every inch of my text and the
web.  It's not easy just "getting it" it has to have the "light go on",
think back to when you first learned it or anything, it's not that i'm not
reading and rereading, it's just that the concepts aren't gelling.  I
apologize for wasting your time, with simple questions, but that's where I
am currently, not due to laziness, but rather just not getting it.

Thanks

Art
rossum - 08 Dec 2007 22:58 GMT
>Does anyone know how to assign an element from an ArrayList to a variable?
Yes.

>Specifically, I know that I can print an item using
>System.out.println(nameList);
Some items you can, others not.

>but how do I assign the ArrayList element to a string variable?
String varble = myArrayList.get(i).toString();

>Thanks
>
>Arrt

[snip]

>/**
>   This program demonstrates the ArrayList toString method.
[quoted text clipped - 6 lines]
>      // Create a vector to hold some names.
>      ArrayList nameList = new ArrayList();
Have you done generics in your classes yet?  In other words, do you
know the difference between:

 ArrayList nameList = new ArrayList();

and

 ArrayList<String> nameListGeneric = new ArrayList<String>();

The first is an ArrayList of Objects the other is an ArrayList of
Strings.  The two are different; only the second is a generic.  When
you assign from the first ArrayList, the compiler only knows that it
contains Objects so you will need to explicitly use toString() to turn
the Object returned into a String.

 String name = nameList.get(1).toString();

When you assign from the second ArrayList, the compiler knows that the
ArrayList<String> contains Strings, so it does not complain when you
assign the returned entry to a String.

 String name = nameListGeneric.get(1);

>  String name;
>
[quoted text clipped - 5 lines]
>      // Now display the items in nameList.
>      System.out.println(nameList);
println takes a string parameter.  You have passed it an ArrayList of
Objects.  So println will implicitly call the toString() method on the
ArrayList you passed.  Since you are attempting to demonstrate the use
of toString(), you might want to call it explicitly:

 System.out.println(nameList.toString());

>   }
>}
Art Cummings - 09 Dec 2007 00:52 GMT
>>/**
>>   This program demonstrates the ArrayList toString method.
[quoted text clipped - 8 lines]
> Have you done generics in your classes yet?  In other words, do you
> know the difference between:

Not yet, just the first one.

>  ArrayList nameList = new ArrayList();
>
[quoted text clipped - 9 lines]
>
>  String name = nameList.get(1).toString();

Thanks Rossum, I did try this but I'm still getting the same compiler error.

¼§ÏArrayListDemo2.java:30: cannot find symbol
ÏϧÏsymbol : variable namelist
ÏϧÏlocation: class ArrayListDemo2

The program compiles until I try to assign the ArrayList element to the
string variable.

Thanks Art
Mark Space - 09 Dec 2007 01:35 GMT
>>> public class ArrayListDemo2
>>> {
>>>   public static void main(String[] args)
>>>   {
>>>      // Create a vector to hold some names.
>>>      ArrayList nameList = new ArrayList();

>>  String name = nameList.get(1).toString();
>
[quoted text clipped - 3 lines]
> ÏϧÏsymbol : variable namelist
> ÏϧÏlocation: class ArrayListDemo2

Hmm, I may be missing something, but doesn't your error message say "I
can't find the variable nameList?"  Rossum used "nameList" because you
used "nameList" in you demo, which I quoted above.

If your program has a different variable than "nameList" you need to use
the correct variable name.
Art Cummings - 09 Dec 2007 02:05 GMT
>>>> public class ArrayListDemo2
>>>> {
[quoted text clipped - 15 lines]
> can't find the variable nameList?"  Rossum used "nameList" because you
> used "nameList" in you demo, which I quoted above.

Thanks Mark, I've been staring at this program for the last 3 days, that was
exactly the problem.

Thanks

Everyone
Art
Mark Space - 09 Dec 2007 02:17 GMT
> Thanks Mark, I've been staring at this program for the last 3 days, that was
> exactly the problem.

You should be using an IDE that tells you when you've spelled a variable
name incorrectly.  I wear reading glasses and sometimes the strain of
reading all day at a terminal takes its toll on me, and I can't find one
letter differences in names either.

Make your life easier and get a decent IDE to help you out.
Art Cummings - 09 Dec 2007 02:57 GMT
> You should be using an IDE that tells you when you've spelled a variable
> name incorrectly.  I wear reading glasses and sometimes the strain of
> reading all day at a terminal takes its toll on me, and I can't find one
> letter differences in names either.
>
> Make your life easier and get a decent IDE to help you out.

Mark I use the one provided by my school.  It's called JGrasp, it's quite a
bit better than the one i'm having to use for my c++ class.  Can you
recommend a better one to get for java?

Thanks

Art
Mark Space - 09 Dec 2007 20:01 GMT
> Mark I use the one provided by my school.  It's called JGrasp, it's quite a
> bit better than the one i'm having to use for my c++ class.  Can you
> recommend a better one to get for java?

I use NetBeans.  It takes a fairly decent machine to run, but it works
well, and it produces standard projects.  That is, they can be compiled
and run from the command line just as easily as within the IDE.

http://sunmicro.vo.llnwd.net/c1/netbeans/6.0/final/

Before you download, you may want to browse through the tutorials and
understand what it will do for you.

http://www.netbeans.org/kb/trails/java-se.html

This one here goes into several features of the Java editor:

http://www.netbeans.org/kb/60/java/editor-tips.html
Art Cummings - 09 Dec 2007 03:18 GMT
> Hmm, I may be missing something, but doesn't your error message say "I
> can't find the variable nameList?"  Rossum used "nameList" because you
> used "nameList" in you demo, which I quoted above.
>
> If your program has a different variable than "nameList" you need to use
> the correct variable name.

Mark, it's amazing, this one little piece has unblocked everything i've been
hitting my head up against.  Thanks again!!!

Art


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.