• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

swaping in array with compareto method

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys i need to write a constructor that make a new object (checks the catalogue number aand sort it by the catalogue number in array)

i am trying to figure out how to put in this method the sort by using compareto method and without using sort.
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't use underscores in your variable names.  Ses Java NamingConventions (that's a link).

What do you want to sort?  The array?  Can you use a List instead?
 
Jenia Levin
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i cant use a list i have to get first object and put it in array 0 then get another one and check if the catalog number is bigger or now if bigger put it from smaall to biggest (0 is the smallest)
without holes in the array
 
Saloon Keeper
Posts: 9740
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'x' is already a FoodItem so I'm curious as to why you are doing new FoodItem(x) ?
 
Jenia Levin
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because every time making new object in next array and comparing the catalog number (every time that a new object is created it checks the other object in array and sort them)
 
Carey Brown
Saloon Keeper
Posts: 9740
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jenia Levin wrote:Hello guys i need to write a constructor...

OK so far

...that make a new object (checks the catalogue number aand sort it by the catalogue number in array)

This doesn't seem like a job for a constructor but more for an add() method of an Inventory class.
 
Carey Brown
Saloon Keeper
Posts: 9740
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jenia Levin wrote:because every time making new object in next array and comparing the catalog number (every time that a new object is created it checks the other object in array and sort them)


Sorting and comparing operations don't seem like appropriate responsibilities for a constructor.

Isn't a constructor already being called to create the FoodItem that you are then passing in to add() ?
 
Jenia Levin
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes youre right my mistake i am was minning about add method the constructor is done (srry my mistake)
 
Carey Brown
Saloon Keeper
Posts: 9740
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm curious (concerned?). Would you please post your entire FoodItem class?
 
Don't play dumb with me! But you can try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic