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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:44:32+00:00 2026-05-27T02:44:32+00:00

I’m having an issue understanding some compiler-errors, regarding 2D arrays (ArrayList containing an ArrayList)

  • 0

I’m having an issue understanding some compiler-errors, regarding 2D arrays (ArrayList containing an ArrayList) and generics. My understanding of generics isn’t the best, so I tried to research the issue beforehand and still ended up confused.

According to comments on 2D dynamic array using ArrayList in Java, you can’t mix arrays with generics (or rather, you can with @SuppressWarnings(“unchecked”), but are discouraged from doing so). However, I’m not sure what this exactly means.

Here is my problem code:

    blocks = new ArrayList<ArrayList<BarrierBlock>>(columns); // initialize rows
    for (int i = 0; i < columns; i++){
        // blocks.get(i) = new ArrayList<BarrierBlock>(rows);  <- ERROR = (unexpected type; required: variable, found: value)
        blocks.add(new ArrayList<BarrierBlock>(rows)); // initialize columns
    }

    // initilize each block
    for (int i = 0; i < blocks.size(); i++){
        for (int j = 0; i < blocks.get(i).size(); j++){
            int[] blockLoc = {location[0] + (i*BLOCK_SIDE_LENGTH), location[1] + (j*BLOCK_SIDE_LENGTH)};
            // blocks.get(i).get(j) = new BarrierBlock(BLOCK_SIDE_LENGTH, blockLoc); <- ERROR = (unexpected type; required: variable, found: value)
            blocks.get(i).add( new BarrierBlock(BLOCK_SIDE_LENGTH, blockLoc)); // initialize 2D array elements
        }
    }

The two lines that I commented out were my initial attempts at initializing the arrays. The compiler complained when I tried this and gave me the error listed. What does this error mean? I would think that both sides are of the declaration statement are variables.

After looking around, I found out that I’m supposed to use the add(E e) method of ArrayList. But what is the main difference? In the new way I’m initializing arrays, doesn’t that also “mix arrays with generics”?

  • 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-27T02:44:33+00:00Added an answer on May 27, 2026 at 2:44 am

    Get RETURNS the object at the given index, it can’t be used to SET the object.

    here are things you CAN do with get:

    list l = new list();
    item a;
    l.add(a);
    item b = l.get(0);
    b.property = 10;
    l.get(0).property == 10; //true, a is the same object as b
    b = new item();
    l.get(0) == b; //false, list[0] is still a, b is now pointing to a different object
    l.get(0) = b; //error, you can't assign to list.get
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, 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.