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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:09:32+00:00 2026-05-11T20:09:32+00:00

ok; i am making a project for school and its a game similar to

  • 0

ok; i am making a project for school and its a game similar to falling sand. but in order for the gravity to work i have to have the sand sorted by its position from down to up (the y variable of sand) this method should sort it; although i cannot get .clone() to work and i can’t hard copy any other way i know. so i don’t know how to replace all the comments in this code with something that will do what they say.

to explain how i want this to work; i want it to, one by one, remove elements from world while i place them sorted in sorted.

   public void sort(){
  //method to sort elements by y value
     ArrayList<sand> sorted=new ArrayList<sand>();
     if(world.size()!=0){
        //code to take 0 from world and place it into sorted at 0
        while(world.size()>0){
           boolean check=true;
           for(int i=0;i<sorted.size();i++){
              if(world.get(0).y<sorted.get(i).y){
                 //code to take 0 from world and place it into sorted at i
                 check=false;
              }
           }
           if(check){
              //code to take 0 from world and place it at the end
           }
        }
     }
     //code to make sorted the contents of world
  }

the error i am getting with clone is:

awesomesand.java:48: clone() has protected access in java.lang.Object
            sand a=world.get(0).clone();

and, yes world is of type sand.


EDIT

now i am getting an error on cloning.

awesomesand.java:48: incompatible types
found   : java.lang.Object
required: awesomesand.sand
            sand a=world.get(0).clone();
                                     ^
  • 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-11T20:09:32+00:00Added an answer on May 11, 2026 at 8:09 pm

    You are getting the clone exception because it has protected access in Object. However, sorting an ArrayList does not need clone() if you invoke the standard sorting mechanisms of Collections.

    As for why you’re getting the clone error, a class must override clone() with public access. This is to assure that you handle the specifics of your class:

    public class Sand implements Cloneable {
        public Object clone() throws CloneNotSupportedException {
            Sand copy = (Sand) super.clone();
            // Sand specific copying done here
            return copy;
        }
    }
    

    However, easier, more efficient, and more likely correct, is to just sort the collection of sand objects directly. Here I’ve defined a possible version of Sand, and show the use of Collections.sort:

    public class Sand {
        public int x;
        public int y;
    }
    
    public class SandComparator implements Comparator<Sand> {
        public int compare(Sand s1, Sand s2) {
            // reverse these to sort in the opposite order
            return s1.y - s2.y;
        }
        public boolean equals(Object o) {
            return o instanceof SandComparator;
        }
    }
    
    public class App {
        ArrayList<Sand> world;
        ...
        public void sort() {
            Collections.sort(world, new SandComparator());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a school project where we are making a small JavaScript game without
I'm making Minesweeper as a school project. It's close to completion, but the only
Im making a table generator as a school project. In MySQL I have 3
I am making a Grails application for a school project and I have run
I'm making a small game for a school project, and basically there are 20
I have this school project I'm making, where I need to make my code
I am making a project posting system that will have somewhat some markup, similar
Im in the making of a little project at school: A client where you
I have a little problem, I'm making project and I'm sending data from client
I'm making a project for my work, and there is a big problem... I'm

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.