|
You're not comparing i to j ; you're comparing a[i] to a[j] .
Look at what happens when i = 0 and j = 1 :
a[i] == 4
a[j] == 2
a[i] > a[j]
=== 4 > 2
=== true
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thank you Richard for your replay in fact, this is my question how on the second loop j start with 1 ? should start with 0 since j always j=i ; and i start with 0 , then j will be 0 so a[i] will be 4 and a[j] will be 4 ?
for(int i=0; i< a.length ;i++) {
for(int j=i; j< a.length ;j++) {
modified 20-Jun-16 14:16pm.
|
|
|
|
|
j doesn't start with 1 ; it starts with i , and then increments for each pass of the inner loop.
i = 0
j = 0 a[i] = 4; a[j] = 4
j = 1 a[i] = 4; a[j] = 2
j = 2 a[i] = 4; a[j] = 7
i = 1
j = 1 a[i] = 2; a[j] = 2
j = 2 a[i] = 2; a[j] = 7
i = 2
j = 2 a[i] = 7; a[j] = 7
You really need to learn to use a debugger. Rather than trying to guess what your code is doing, step through it line by line and examine the values of the variables.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I came over this post again , it surprise for me to not see my last post ,
any way, Dear Richard thank you so much for clearing the process steps of loop , I really appropriate your time. and i'm sorry to replay back late i just shack to not see my thank you post I may closed the browser before public the post
![Rose | [Rose]](https://www.codeproject.com/script/Forums/Images/rose.gif)
|
|
|
|
|
There're 9 numbers in a 3 x 3 plane. We could only clockwise or counterclockwise rotate the four tiles around one of the four points, I II III and IV. R1, R2, R3, R4 and r1, r2, r3, r4 are corresponding to the 8 kinds of rotation, where we mark clockwise rotations as r and counterclockwise rotations as R. For example, starting from configuration (S), by the rotation sequence r1R4r2R3 we reach the configuration (E). Now, starting from configuration (S), find the shortest way to reach configuration (T).
If I want to program to solve the problem,do you have any good idea?
[Cilck here for detail picture]
|
|
|
|
|
Your homework is set to test what you know, not how good you are at getting strangers to do your work for you.
Try it yourself. It should be based on concepts that you have recently covered in your course, so you'll probably find it's actually not as difficult as you think.
If you don't know where to start, then talk to your teacher.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
that't not homework,my friend ask me the question, I think it's funny, so I want to solve it with an Android app,traversal need too much steps,so I need an idea about math models
|
|
|
|
|
zarkerBlack wrote: so I need an idea about math models Then you should use Google to do some research. This forum is for Java questions.
|
|
|
|
|
I already have done the work like this ,the real problem is about machine learning,how could the machine konw the way to the result,I have three ideas,one is let machine try all the ways,another is find the law,it like the deduction of mathematic,the last one is that,let machine think like man,it could do the work like a real person ,maybe i need to design a classifier like SVM to training the machie。
Do you have any advice about last two?
|
|
|
|
|
As I explained in my previous message, this has nothing to do with Java. You need to do some research on machine learning.
|
|
|
|
|
I have class:
public class book implements Serializable {
private static final long serialVersionUID = 1L;
private String title = "N N";
private String author = "N N";
private int price = 0;
public book()
{
}
public book(String title, String author, int price)
{
setTitle(title);
setAuthor(author);
setPrice(price);
}
public void setTitle(String title)
{
this.title = title;
}
public void setAuthor(String author)
{
this.author = author;
}
public void setPrice(int price)
{
this.price = price;
}
public int getPrice()
{
return price;
}
public String getTitle()
{
return title;
}
public String getAuthor()
{
return author;
}
public void skrivUt()
{
System.out.println("");
System.out.println("Titel: " + title);
System.out.println("Författare: " + author);
System.out.println("Pris: " + price);
}
}
And then a list to which I save the objects:
book aBook = new book(title, anAuthor , thePrice);
aList = new ArrayList<book>();
aList.add(aBook);
I save the list:
FileOutputStream fil = new FileOutputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectOutputStream oostr = new ObjectOutputStream(fil);
oostr.writeObject(aList);
oostr.close();
And gets it back:
FileInputStream fil = new FileInputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectInputStream oistr = new ObjectInputStream(fil);
aList = (ArrayList<book>) oistr.readObject();
oistr.close();
Goes through the list in a textArea:
for (book b : aList) {
ta1.append("Titel " + b.getTitle() + "\n");
ta1.append("Författare: " + b.getAuthor() + "\n");
ta1.append("Pris: " + Integer.toString(b.getPrice()) + "\n");
ta1.append("\n");
}
This works fine. The problem is that I only get back (or so it seems) the first object in the list.
|
|
|
|
|
I just ran a slightly modified version of your code and it returned all the items. I did find that the object reader did not return the list as an ArrayList<Book> (maybe my mistake) so had to modify the foreach to accept a List of objects and cast each one to a Book.
[edit]
A slight modification shows that your code should work fine, and return the full list.
[/edit]
modified 14-Jun-16 10:43am.
|
|
|
|
|
Thanks for your reply.
Could you show me some code?
I tested to add some objects by writing in the textfields and pressing the button (btn).
If I do this for some books and then hit the second button (btn2) it only shows the first book in the list.
If I add a book and then hits the second button it show that book. If I add a second book and hits the second button again, it shows both books.
|
|
|
|
|
That is different from your original question. The code that works is the code I copied from your question, and the only changes I made are:
ArrayList<Book> bList = (ArrayList<Book>) oistr.readObject();
oistr.close();
for (Book bb : bList) {
bb.skrivUt();
}
which worked, and listed all the items I added to the original list.
You are now asking something concerned with pressing buttons, which you have not shown in your code.
|
|
|
|
|
 Ok, I'll try again.
This is my total code in a class that inherits from JFrame instantiated from main(). Slightly changed because I come from Sweden.
So I enter the info in the textfield for title, author and price.
If I enter the info for one book and then press "show books" it works. I enter another book and press show books again it still works. The textarea shows all books.
If I enter a book and doesn't press "show books", enter a few others and then hit "show books" it only shows the last entered book.
If I have entered a few books using the first method (enter book, press "show books", enter another book, press show books again)and then quits using the Close-button one books is saved, not more.
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class MainFrame extends JFrame implements ActionListener, MyContacts {
private static final long serialVersionUID = 1L;
public JTextField txtFieldTitle;
private JTextField txtFieldAuthor;
private JTextField txtFieldPrice;
private JTextField txtFieldLanguage;
private JTextArea ta1;
private JButton btn;
private JButton btn2;
private JButton btnClose;
private JButton btnRemove;
private JRadioButton proRadio;
private JRadioButton amateurRadio;
private ButtonGroup playersBtnGroup;
private ArrayList<book> aList;
private JLabel txtTextArea;
private JLabel txtTitle;
private JLabel txtAuthor;
private JLabel txtPrice;
private Border redBorder = BorderFactory.createLineBorder(Color.red);
public MainFrame(){
super("Library");
setLayout(null);
setSize(1200, 1000);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
getContentPane().setBackground(Color.red);
loadBook();
btn = new JButton("Add book");
btn.setBounds(120,70,200,20);
add(btn);
btn.addActionListener(this);
btn2 = new JButton("Show books");
btn2.setBounds(220,470,200,20);
add(btn2);
btn2.addActionListener(this);
btnClose = new JButton("Close");
btnClose.setBounds(150,800,200,50);
add(btnClose);
btnClose.addActionListener(this); händelser
txtFieldTitle = new JTextField();
txtFieldTitle.setBounds(570,70,200,20);
add(txtFieldTitle);
txtFieldAuthor = new JTextField();
txtFieldAuthor.setBounds(570,110,200,20);
add(txtFieldAuthor);
txtFieldPrice = new JTextField();
txtFieldPrice.setBounds(570,150,200,20);
add(txtFieldPrice);
ta1 = new JTextArea();
ta1.setBounds(720,470,400,420);
ta1.setBorder(redBorder);
add(ta1);
txtTextArea = new JLabel("Böcker");
txtTextArea.setBounds(720,430,200,20);
add(txtTextArea);
txtTitle = new JLabel("Titel");
txtTitle.setBounds(500,70,200,20);
add(txtTitle);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btn)
{
String title = txtFieldTitle.getText();
String anAuthor = txtFieldAuthor.getText();
String anPrice = txtFieldPrice.getText();
String aLanguage = txtFieldLanguage.getText();
int thePrice = Integer.parseInt(anPrice);
book aBok = new book(title, anAuthor , thePrice);
aList = new ArrayList<book>();
aList.add(aBok);
}
if(e.getSource() == btn2)
{
for (book b : aList) {
ta1.append("Titel " + b.getTitle() + "\n");
ta1.append("Författare: " + b.getAuthor() + "\n");
ta1.append("Pris: " + Integer.toString(b.getPrice()) + "\n");
ta1.append("\n");
}
}
if(e.getSource() == btnClose)
{
saveBook(aList);
System.exit(0);
}
}
public void saveBook(ArrayList<book> aList)
{
try
{
FileOutputStream fil = new FileOutputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectOutputStream oostr = new ObjectOutputStream(fil);
oostr.writeObject(aList);
oostr.close();
}
catch (IOException e)
{
System.out.println(e);
System.out.println("Nu blev det visst fel i spara Bok");
}
}
@SuppressWarnings("unchecked")
public void loadBook()
{
try
{
FileInputStream fil = new FileInputStream("C:\\SavedObjects\\Objektfil.dat");
ObjectInputStream oistr = new ObjectInputStream(fil);
aList = (ArrayList<book>) oistr.readObject();
oistr.close();
}
catch (IOException e)
{
System.out.println(e);
System.out.println("Something went wrong in IO");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
System.out.println("Could not find class...");
}
}
}
|
|
|
|
|
if(e.getSource() == btn)
{
String title = txtFieldTitle.getText();
book aBok = new book(title, anAuthor , thePrice);
aList = new ArrayList<book>();
aList.add(aBok);
}
Every time you add a book you create a new ArrayList<book>() , so you are losing the previous books. You need to create your list at the beginning of the program and add to that as you enter more books.
|
|
|
|
|
Ah, of course!
Thank you, so simple
So, again, thank you very much!
So can I mark as solved or give you credit?
|
|
|
|
|
You can upvote my answer if you like. But I do this as a hobby, so your message of thanks is enough.
|
|
|
|
|
Well, are very greatful.
Don't really know how to upgrade, could you tell me?
Also, I have another question and hope it is ok to ask it here.
When I start the window it is blank or only a few items. When I minimize the window and maximaze it again, everything is fine.
Do you know why this is?
|
|
|
|
|
larsp777 wrote: Don't really know how to upgrade, could you tell me? Er, upgrade what?
larsp777 wrote: When I start the window it is blank or only a few items. Sounds like your startup code is not forcing all items to be painted; suggest you open a new question with all the details.
|
|
|
|
|
I meant how can I upgrade your answer to give you credit
Sure I can start a new question if you want.
All I do though is call the class I shown in main like this:
new MainFrame();
And then I inherit from the main window like:
public class MainFrame extends JFrame implements ActionListener {
So most code is in the constructor.
So should I post a new question?
|
|
|
|
|
larsp777 wrote: how can I upgrade your answer Sorry, I misunderstood, as the word "upgrade" has a slightly different meaning. If you open any message and move your mouse just to the left of the text near the top, you should see two coloured arrows. A green one pointing up, and a red one pointing down. Click the green one to upvote the message, or the red one to downvote (means you think it is not a good message).
As to your other problem, I think we need to see the code, so I suggest you open a new question.
|
|
|
|
|
I meant upvote, sorry.
For my other question I think I shown all code, but can post if again of course.
Anyway I think I found a solution.
Added this:
repaint();
revalidate();
Seem to work.
Not sure if the order matters.
|
|
|
|
|
Whoa! before i thought i know Java but when i saw your code about serialization i realized i need to study more. I have never come across it. may be because I am just at intermediate level. your organization of class is what so much impress me about your codes. thank you so much. I wish I can get the complete source code for this.
|
|
|
|
|
There is no "complete source code", you have to write your own implementations. However, there are plenty of samples and tutorials that Google will find for you.
[edit]
See java serialization - Google Search[^]
[/edit]
|
|
|
|
|