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

  • Home
  • SEARCH
  • 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 6204127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:02:09+00:00 2026-05-24T05:02:09+00:00

I already asked this question and I got additional question about using class I

  • 0

I already asked this question and I got additional question about using
class I made myself.
see code below.

  import java.util.*;

    class Pair{
      int toWhere;
      int weight;
    }

    public class Test{
      public static void main(String[] args){
        ArrayList[] arr = new ArrayList[2];
        Pair p = new Pair();

        for(int i=0; i<arr.length; i++)
          arr[i] = new ArrayList<Pair>();

        p.toWhere = 1;
        p.weight = 2;
        arr[0].add(p);
        System.out.println(p); // gives me Pair@525483cd
        System.out.println(arr[0].get(0)); // gives me exactly the same, Pair@525483cd
        System.out.println(p.toWhere); // gives me no error, and is 1
        System.out.println(arr[0].get(0).toWhere); // gives me an error
      } 
    }

my question is this.
values of p and arr[0].get(0) (which is address? I guess) is the same.
but why does p.toWhere give me the accurate value and
arr[0].get(0).toWhere does not?

  • 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-24T05:02:11+00:00Added an answer on May 24, 2026 at 5:02 am

    That’s because the compiler doesn’t know that arr is an array of ArrayList of Pair. You need to type arr:

    List<Pair>[] arr = new ArrayList[2];
    

    Now when you use arr[0].get(0), the compiler knows that get returns a Pair (not an Object as in your code), so Pair‘s methods are available.

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

Sidebar

Related Questions

I have already asked this question on ServerFault however I got very minimal replies....one
I have asked this question about using the a Linq method that returns one
I have already asked this question in offical Flot google groups, but got no
I already asked this question at the JOS-.NET board but Joel is closing that
I am sorry I have already asked this question on Superuser, but nobody answers
I've seen this question asked already - but none of the answers really gelled
Searching here I found that this question was already asked , but I think
If this question has already been asked I appologies, please point me in the
Edit : This question has already been asked and answered, and I apparently am
I'm sorry if this question has been asked already, but I couldn't find it

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.