Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:52:25+00:00 2026-06-01T10:52:25+00:00

I have taken this example from net . But when i tried it is

  • 0

I have taken this example from net .
But when i tried it is not compiling saying cannot convert Object to String

import java.util.ArrayList;

public class Test {

    public static void main(String[] args) {
        ArrayList names = new ArrayList();

        names.add("Amy");
        names.add("Bob");
        names.add("Chris");
        names.add("Deb");
        names.add("Elaine");
        names.add("Frank");
        names.add("Gail");
        names.add("Hal");

        for (String nm : names)
            System.out.println((String)nm);

    }
}

If it is a normal for loop i could have done list.get(element index).toString() .
but how to do in enhanced for loop ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:52:26+00:00Added an answer on June 1, 2026 at 10:52 am

    You shouldn’t bypass type safety by calling toString() – you should use generics to start with:

    List<String> names = new ArrayList<String>();
    

    Now your for loop will compile (you can get rid of the cast in the System.out.println call btw) and the compiler will prevent you from adding a non-string to your list.

    See the Java generics tutorial for a starting point on generics, and the Java Generics FAQ for more information that you’ll ever want to know 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following example is taken from: http://php.net/manual/en/function.curl-multi-close.php#example-3540 This example will create two cURL handles,
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
I've tried some googling on this subject but i would like to have som
I have a url like this - www.example.com/blog.php/username, it will take you to the
Take a very simple case as an example, say I have this URL: http://www.example.com/65167.html
Im new to iPhone development and I have really taken this to me. I
I'm not sure if something odd is happening or if this is normal, but
I have a .net web service that takes some xml data. From within the
I've taken inspiration from the example show in the following URL csharp-online and intended
i searched a lot for an answer to this question but i did not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.