The Artima Developer Community
Sponsored Link

Java Answers Forum
simple problem but need help

3 replies on 1 page. Most recent reply: Feb 23, 2003 9:03 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
LS

Posts: 1
Nickname: beginner
Registered: Feb, 2003

simple problem but need help Posted: Feb 19, 2003 8:14 PM
Reply to this message Reply
Advertisement
I am trying to use two one-dimensional arrays to create a list of videos and ID numbers. I have to print the list and then also sort the list. Can anoyone help me make this right?
public class Video{public static void main(String[] args) throws exception
{
int x,y;
int[] ID = {212,17,30, 461,49, 15};
String[] Titles= {"Necessary Roughness","Top Gun","Sleepless in Seattle", "Rudy"};
System.out.println("List before Sort");
for(x = 0; x < Titles.length; ++x)
System.out.println(ID[x]);
SortStrings(
System.out.println("\nAfter Sort:");
for(int i = 0; i < Titles.length; i++)
System.out.println("ID:" + ID + "\tTitle: " + Titles);}
}


mihai

Posts: 2
Nickname: mishoo
Registered: Feb, 2003

Re: simple problem but need help Posted: Feb 23, 2003 1:39 AM
Reply to this message Reply
what happened to the SortList function?
what are its parameters?
does it have any?

and also the indentation is missing, that helps a lot in a program (the brackets are incorrect in some places)

mihai

Posts: 2
Nickname: mishoo
Registered: Feb, 2003

Re: simple problem but need help Posted: Feb 23, 2003 1:40 AM
Reply to this message Reply
what happened to the SortString function?
what are its parameters?
does it have any?

and also the indentation is missing, that helps a lot in a program (the brackets are incorrect in some places)

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: simple problem but need help Posted: Feb 23, 2003 9:03 AM
Reply to this message Reply

import java.util.*;
public class VideoSorter{

public static void main(String[] args) {
VideoSorter sorter = new VideoSorter();
int[] ids = {212, 17, 30, 461, 49, 15};
String[] titles= {
"Necessary Roughness",
"Top Gun",
"Sleepless in Seattle",
"Rudy",
"One More",
"Another One"};
List videos = new ArrayList();
/* Add Video objects to the List */
for (int i = 0; i < titles.length; i++){
Video video = new Video(ids[i], titles[i]);
videos.add(video);
}
/* Display the list of Video objects before sort */
System.out.println("List before Sort");
for (int i = 0; i < titles.length; i++){
Video nextVideo = (Video) videos.get(i);
System.out.println(nextVideo.toString());
}
Collections.sort(videos, (Comparator)videos.get(0));
System.out.println("\nAfter Sort:");
for (int i = 0; i < titles.length; i++){
Video nextVideo = (Video) videos.get(i);
System.out.println(nextVideo.toString());
}
}

static class Video implements Comparator{
private int id;
private String title;

public Video(int id, String title){
this.id = id;
this.title = title;
}

public int getID(){
return id;
}
public String getTitle(){
return title;
}

public String toString(){
return "ID: " + String.valueOf(getID()) + "\t" + "Title: " + getTitle();
}


public int compare(Object o1, Object o2){
Video video1 = (Video)o1;
Video video2 = (Video)o2;
return (new Integer(video1.getID())).compareTo(new Integer(video2.getID()));
}

public boolean equals(Object obj){
Video video = (Video)obj;
return (new Integer(this.getID())).equals(new Integer(video.getID()));

}
}

}

Flat View: This topic has 3 replies on 1 page
Topic: Java and XML Previous Topic   Next Topic Topic: How Vendors Implement of Java Api and Java interfaces

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use