Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Iterating through an ArrayList to view its contents

  1. #1
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    53
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Iterating through an ArrayList to view its contents

    Yeah, I'm trying to figure out how to print the elements of an array list in a non-static way. Here's the array list function definition class:
    package samsExperiments;
    import java.util.ArrayList;
     
    public class SamsArrays {
     
    	public static ArrayList<String> samsStaticArrayList() {
    		ArrayList x = new ArrayList();
    		x.add("A");
    		x.add("B");
    		x.add("C");
    		x.add("D");
    		return(x);		
    	}
     
    	public ArrayList<String> samsArrayList() {
    		ArrayList x = new ArrayList();
    		x.add("A");
    		x.add("B");
    		x.add("C");
    		x.add("D");
    		return(x);		
    	}
    }

    And here's the class with the main method:
    package samsExperiments;
     
    import java.util.Scanner;
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.Random;
     
    import static samsExperiments.StaticMethodExample.*;
     
    import java.lang.StringBuilder;
    import java.text.DecimalFormat;
    import java.util.Arrays;
    import java.util.InputMismatchException;
     
    import customExceptions.IntegerOutOfRangeException;
     
    public class SamsExperimentsMain {
     
    	public static void main(String[] args){
     
    		SamsArrays.samsStaticArrayList();//call a static method
     
    		SamsArrays sams = new SamsArrays();
    		ArrayList<String> x = sams.samsArrayList();
     
    		for(int i = 0; i < x.length(); i++) {
    			System.out.println(x[i]);
    		}		
    	}
    }

    The error on line 26 says, "The method length() is undefined for the type ArrayList<String>".
    The error on line 27 says, "The type of the expression must be an array type but it resolved to ArrayList<String>".

    How do I fix this?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,322
    Thanks
    53
    Thanked 2,393 Times in 2,347 Posts

    Default Re: Iterating through an ArrayList to view its contents

    "The method length() is undefined for the type ArrayList<String>".
    The compiler can not find a definition for the length method in the ArrayList class. Read the API doc for ArrayList to see what method returns the value you want.
    The API doc link: http://docs.oracle.com/javase/8/docs/api/index.html

    The type of the expression must be an array type but it resolved to ArrayList<String>
    The [] notation is used with array. The ArrayList has methods to access its contents. Read the API doc to see what method to use.

    Also see the tutorial:
    https://docs.oracle.com/javase/tutor...ts/arrays.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2018
    Location
    Wisconsin
    Posts
    53
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Iterating through an ArrayList to view its contents

    Problem Solved!
    public ArrayList<String> samsArrayList() {
    		ArrayList x = new ArrayList();
    		x.add("A");
    		x.add("B");
    		x.add("C");
    		x.add("D");
    		return(x);		
    	}
    	/*
    	//How to call it in the main method
    	SamsArrays sams = new SamsArrays();
    	ArrayList<String> x = sams.samsArrayList();
     
    	for(String str:x) {
    		System.out.println(str);
    	}
    	*/

Similar Threads

  1. ArrayList and selecting random contents
    By rmpslkkr in forum Collections and Generics
    Replies: 0
    Last Post: September 20th, 2018, 07:06 AM
  2. How to View contents of .ISO file in java?
    By zakhilton in forum Java Theory & Questions
    Replies: 3
    Last Post: July 15th, 2014, 08:02 AM
  3. Help making and iterating through ArrayList (Java Beginner)
    By Sammis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 20th, 2013, 06:58 AM
  4. [SOLVED] Printing arraylist contents in Jtextarea
    By Johnny Bravo in forum AWT / Java Swing
    Replies: 4
    Last Post: August 31st, 2012, 08:56 AM
  5. [SOLVED] Iterating through an ArrayList
    By Actinistia in forum Collections and Generics
    Replies: 2
    Last Post: October 31st, 2011, 02:19 PM