|
Nagy Vilmos wrote: Also using an array clone is the most efficient way to do it.
It's not a question of efficiency.
coeff = other.coeff.clone();
does something fundamentally different from
coeff = other.coeff;
In fact you could argue that the second form is more efficient since it creates fewer objects at runtime. But in this case it is probably not what we want to do so it is just plain wrong, never mind whether it is efficient or not.
|
|
|
|
|
The second copies the array reference, a shallow copy, and means that changing the content in the new instance changes the old one too.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Yes, but what has that got to do with efficiency? I think you are confusing "efficiency" with "correctness".
|
|
|
|
|
The most efficient way to clone an array.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
clone is faster because it only does a shallow copy of the array. So if you are working with a one-dimensional array of primitives (as in this case) you will get away with it. But for other cases it might not be what you want (two-dimensional arrays don't get cloned properly, for example). For copying arrays you're better off using Arrays copyOf or System arrayCopy. Again, it's a question of correctness not efficiency. Is it efficient to be fast but wrong?
|
|
|
|
|
It's a primitive array, that's why using a simple clone is the right approach in this case.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
So that's why you described clone as a "deep" copy in your original reply, then?
|
|
|
|
|
Okay, I sit corrected. Looking further Array.copy(...) is a better option.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
lol yea the teacher wants equals(Polynominal p) so is my way right or wrong?
|
|
|
|
|
looks good at a first glance - needs some fixes (read the comments).
How do you programm? Do you use a IDE like Eclipse or Netbeans?
regards Torsten
I never finish anyth...
|
|
|
|
|
Accessing private members that way? at least bad style.
my suggestion:
public Polynomial(Polynomial p)
{
coeff = p.getCoeff();
}
regards Torsten
I never finish anyth...
|
|
|
|
|
There is no method getCoeff() , just getCoeff(int) .
Seriously, I don't think accessing member variables in a constructor is necessarily bad, but I guess OP could go for the 'collapse all constructors into one' approach:
public Polynomial(Polynomial p)
{
this(p.getCoeff(0),
p.getCoeff(1),
p.getCoeff(2),
p.getCoeff(3),
p.getCoeff(4));
}
public Polynomial()
{
this(0, 0, 0, 0, 0);
}
public Polynomial(int c0, int c1, int c2, int c3, int c4)
{
this.coeff = new int[5];
this.coeff[0] = c0;
this.coeff[1] = c1;
this.coeff[2] = c2;
this.coeff[3] = c3;
this.coeff[4] = c4;
}
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
TorstenH. wrote: Please use decent Hungarian Code conventions:
Without opening a can of worms as to the pros and cons of Hungarian notation, I wouldn't teach a Java newbie to use Hungarian for the simple reason that most Java coders do not use it so it will just confuse people. Sun published a set of standards years ago which still pretty much form the basis of 99.9% of java shops' coding conventions in the real world, so I would advise newbies to go and read that and follow it.
|
|
|
|
|
I know that most people do not use Hungarian Code Conventions for their code. But I think it makes programming especially for students simpler. Using clear naming for the variables and methods makes it much easier to deal with them.
regards Torsten
I never finish anyth...
|
|
|
|
|
As I said, I don't really want to get into the pros and cons of Hungarian notation. It's one of those religious discussions that usually descends into a pointless to-and-fro "my dad's bigger than your dad" kind of argument. It ranks somewhere alongside the question of whether opening braces should be placed on the same line or the following line.
|
|
|
|
|
naaa, it's not that kind of discussion.
I always see beginners using non-unique names for their variables and members like "b1, b2, b3, ..". It get's worse when it comes to list and labels, which are often named the same way: "l1, l2, l3, ...".
It'S so easy to get that cleared up! One can mess around when he's a pro and does code no one cares of. But as soon as a second person has to deal with the code, one should take a seconds time to think about.
EDIT: Check his new thread: http://www.codeproject.com/Messages/4032381/Polynomial-class-need-some-assistance-with-it.aspx[^]
He's using an int "coeff" in his main function and has the member Array[] "coeff" in the object he's dealing with. Technically allowed - but not helping at all.
regards Torsten
I never finish anyth...
|
|
|
|
|
Hello again I am creating again a new app in java which can animate images. I have problem with JRadionButton if I am going to select another one it marked with a dot and if I select another one it also marked with a dot without removing the pass which I had selected.
Here's my code
package star;
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
public class StarAnimate extends JFrame implements ItemListener {
JPanel panel = new JPanel();
JRadioButton btnBoard1 = new JRadioButton("Board 1", true);
JRadioButton btnBoard2 = new JRadioButton("Board 2");
JRadioButton btnBoard3 = new JRadioButton("Board 3");
Board board1 = new Board();
Board2 board2 = new Board2();
Board3 board3 = new Board3();
public StarAnimate() {
init();
}
private void init() {
btnBoard1.setBounds(5, 10, 100, 30);
btnBoard1.setBackground(Color.red);
btnBoard1.addItemListener(this);
btnBoard2.setBounds(5, 40, 100, 30);
btnBoard2.setBackground(Color.red);
btnBoard2.addItemListener(this);
btnBoard3.setBounds(5, 70, 100, 30);
btnBoard3.setBackground(Color.red);
btnBoard3.addItemListener(this);
panel.setLayout(null);
panel.setBounds(0, 0, 160, 540);
panel.setBackground(Color.red);
panel.add(btnBoard1);
panel.add(btnBoard2);
panel.add(btnBoard3);
this.add(panel);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(560, 540);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setTitle("Star Animation");
this.setResizable(true);
this.setVisible(true);
}
public static void main(String[] args) {
StarAnimate star = new StarAnimate();
}
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getSource() == btnBoard1) {
this.add(board1);
this.remove(board2);
this.remove(board3);
}
if (e.getSource() == btnBoard2) {
this.add(board2);
this.remove(board1);
this.remove(board3);
}
if (e.getSource() == btnBoard3) {
this.add(board3);
this.remove(board2);
this.remove(board1);
}
}
}
|
|
|
|
|
Add your JRadioButton into a JRadioButtonGroup
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.
My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done!
|
|
|
|
|
Lol this is it thanks. Anyway its just ButtonGroup not JRadioButtonGroup.
|
|
|
|
|
flashery wrote: Anyway its just ButtonGroup not JRadioButtonGroup.
Sorry about that, my brain's currently running in safe-mode (ie. debugging some VB apps)
Excuse me for my improper grammar and typos.
It's because English is my primary language, not my first language.
My first languages are C# and Java.
VB, ASP, JS, PHP and SQL are my second language.
Indonesian came as my third language.
My fourth language? I'm still creating it, I'll let you know when it's done!
|
|
|
|
|
|
|
|
Hello again, its been a while since I post here I am making a Timer application in java. Which shutdown the PC on specified time.
My problem is it is showing a warning
Note: C:\Users\flashery\Documents\NetBeansProjects\Timer\src\MainTimer.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
How do I do this recompiling?
Can someone give me a hint on this thing..
|
|
|
|
|
Just add that option to your javac command, or project options in NetBeans. Alternatively find the deprecated method you are calling and change it for the latest version; the documentation will tell you how.
BTW the link in your title links back here.
|
|
|
|