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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:37:16+00:00 2026-05-25T19:37:16+00:00

First of all, my problem is pretty much the same as described there: Array

  • 0

First of all, my problem is pretty much the same as described there: Array returning same values for all indexes except it’s for Java and not C#. (Which I heard were pretty similar however)

A few classes are in play here, I will try to give out the most details:

SomeInterface: An interface for different types of pixels.

PixelTypeA: One type of pixel.

PixelTypeB: Another type of pixel.

(PixelTypeA and PixelTypeB implement/inherit from SomeInterface)

PixelImage: That’s the problematic class, it contains the two-dimensionnal array that has the same values for all squares in it, here is the declaration:

SomeInterface[][] pixels;

Now, the constructor does the following to allocate memory (height and width are parameters):

pixels = new SomeInterface[height][width];

So far so good. But then, a method in PixelImage is called to load pixels from a PPM file. That code works correctly because if I test the value of a pixel right after it’s saved in memory (in the readfile loop), it is alright. However, I realized that after each loop iteration (i.e. after saving each pixel in memory), all the values of the pixels array have the same value, and that is the value of the last pixel saved. Here is the important code in the loop:

pixels[row][col] = new PixelTypeA( pixelValue );

As I said, if I do a system print of pixels[row][col] on the next line, it has the right value in it, though every other index in the pixels two-dimensionnal array has that value too. row and col get incremented correctly. I’m quite new to Java and my assumption is that I missed something regarding references or something of the like, which is causing me to update all of the pixels array accidentally at each iteration of the readfile loop (or might it be the declaration/initialization that is the problem?). I tried to solve this by myself for several hours, but this has become really frustrating. I changed the names and cannot give you complete code since this is an assignment and I want to do it by myself, I just need clues as what might cause that weird behaviour right now.

Thank you!

  • 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-25T19:37:17+00:00Added an answer on May 25, 2026 at 7:37 pm

    You most likely reuse the object you insert in the array, instead of doing a new every time.

    This mean that every array entry points to the same object which is set to the latest value read.

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

Sidebar

Related Questions

First of all there is a partial question regarding this, but it is not
I have sort of a tricky problem I'm attempting to solve. First of all,
Problem: How many of the first 100,000,000 hexagonal numbers are divisible by all the
Problem on WindowsXP (likely will happen on all Win installs), first time using Mercurial.
First of all, I know how to build a Java application. But I have
First of all: I am not an experienced ClearCase user, but I have lots
The title, while long, pretty much says it all. What I have is a
Greetings, people. I'm diving into Objective-C for pretty much the first time. It's kicking
EDIT: Based on evolution of the problem, I edited this question. First of all,
First of all, I don't need a textual comparison so Beyond Compare doesn't do

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.