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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:11:40+00:00 2026-05-26T09:11:40+00:00

I have an ArrayList<float[]> inside which I am placing arrays of floats storing the

  • 0

I have an ArrayList<float[]> inside which I am placing arrays of floats storing the cartesian values of a line ie (x0,y0, x1,y1).

every time I perfrom a .contains(), with a float array it returns false, even though I can see in the debugger that it exists. SO seems to be comparing the memory reference and not the actual values. Any way to make them compare the values?

public static void main (String[] args) {
    ArrayList <float[]>drawnLines = new ArrayList<float[]>();
    float[] line = new float[4];

    line[0] = (float)5;
    line[1] = (float)12;
    line[2] = (float)55;
    line[3] = (float)66;

    drawnLines.add(line);

    float[] linea = new float[4];

    linea[0] = (float)5;
    linea[1] = (float)12;
    linea[2] = (float)55;
    linea[3] = (float)66;

    if (drawnLines.contains(linea)) {
    System.out.println("contians");
    }
    else {
    System.out.println(" does not contian");
    }
}
  • 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-26T09:11:40+00:00Added an answer on May 26, 2026 at 9:11 am

    This is because line.equals(linea) is false.

    You need to wrap the float[] with a class which defines what you mean by equal.

    However, it appear that using a class like Line would be a better choice.


    public static void main(String[] args) {
        List<Line> drawnLines = new ArrayList<Line>();
        drawnLines.add(new Line(5, 12, 55, 66));
        Line linea = new Line(5, 12, 55, 66);
        if (drawnLines.contains(linea))
            System.out.println("contains " + linea);
        else
            System.out.println(" does not contain " + linea);
    }
    
    static class Line {
        final float x0, y0, x1, y1;
    
        Line(float x0, float y0, float x1, float y1) {
            this.x0 = x0;
            this.y0 = y0;
            this.x1 = x1;
            this.y1 = y1;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Line line = (Line) o;
            if (Float.compare(line.x0, x0) != 0) return false;
            if (Float.compare(line.x1, x1) != 0) return false;
            if (Float.compare(line.y0, y0) != 0) return false;
            return Float.compare(line.y1, y1) == 0;
        }
    
        @Override
        public String toString() {
            return "Line{" + "x0=" + x0 + ", y0=" + y0 + ", x1=" + x1 + ", y1=" + y1 + '}';
        }
    }
    

    prints

    contains Line{x0=5.0, y0=12.0, x1=55.0, y1=66.0}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList<Double> , the values of which are converted into slices of
I have an arraylist that gets different type of values in it, 1st value->
I have an arraylist which has a class datastructure. public class empRec { public
I have list of files stored inside arraylist that I need to download in
I have an ArrayList<String> that I'd like to return a copy of. ArrayList has
I have a ArrayList made up of different elements imported from a db, made
I have an ArrayList<String> , and I want to remove repeated strings from it.
I have an arraylist that contains items called Room. Each Room has a roomtype
I have an arraylist set up. I have input instuctions set up too, so
I have an ArrayList that I want to use to hold RaceCar objects that

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.