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 7626199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:17:38+00:00 2026-05-31T05:17:38+00:00

We’ve not covered ArrayLists only Arrays and 2D arrays. What I need to do

  • 0

We’ve not covered ArrayLists only Arrays and 2D arrays. What I need to do is be able to read from an ArrayList from another class. The main aim is to read from them in a for loop and use the values stored in them to display items. However, I have made this quick program to test it out and keep getting this error

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(ArrayList.java:604)
    at java.util.ArrayList.get(ArrayList.java:382)
    at Main.Main(Main.java:14)

Here is my code

import java.util.ArrayList;

public class Main
{
    public static void Main()
    {
        System.out.println("Test");
        ArrayList <Objects> xcoords = new ArrayList<Objects>();

        for( int x = 1 ; x < xcoords.size() ; x++ )
        {
            System.out.println(xcoords.get(x));
        }
    }
}

And then the class where the ArrayList is

import java.util.ArrayList;

public class Objects
{
    public void xco()
    {
        ArrayList xcoords = new ArrayList();
        //X coords
        //Destroyable
        xcoords.add(5);
        xcoords.add(25);
        xcoords.add(5);
        xcoords.add(5);
        xcoords.add(25);
        xcoords.add(5);
        //Static Walls
        xcoords.add(600);
        xcoords.add(400);
        xcoords.add(600);
    }
}

If someone can point me in the correct direction it would be so valuable. I’ve tried to debug however I can get anything helpful.

Thanks in advance.

  • 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-05-31T05:17:39+00:00Added an answer on May 31, 2026 at 5:17 am

    Strictly speaking, the exception is due to indexing location 1 of an ArrayList with 0 elements. Notice where you start you for loop index variable x. But consider this line:

    ArrayList <Objects> xcoords = new ArrayList<Objects>();
    

    xcoords points to a new, empty ArrayList, not the one you created in class Objects. To get that ArrayList, change the method xco like

    public ArrayList<Integer> xco() { // make sure to parameterize the ArrayList
        ArrayList<Integer> xcoords = new ArrayList<Integer>();
    
        // .. add all the elements ..
    
        return xcoords;
    }
    

    then, in your main method

    public static void main(String [] args) { // add correct arguments
    
        //..
        ArrayList <Integer> xcoords = (new Objects()).xco();
    
        for( int x = 0 ; x < xcoords.size() ; x++ ) { // start from index 0
            System.out.println(xcoords.get(x));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.