60 likes | 420 Views
Managing Data Databases Arrays, Lists, and File I/O An Array-based database public static String Artist[] = new String[ 5 ]; public static String Song[] = new String[ 5 ]; public static int length = 0 ; public static void main ( String [] args) { length = Artist.length;
E N D
Managing Data Databases Arrays, Lists, and File I/O
An Array-based database public static String Artist[] = new String[5]; public static String Song[] = new String[5]; public static int length = 0; public static void main(String [] args) { length = Artist.length; Artist[0] = "System of a Down"; Song[0] = "Hypnotize"; Artist[1] = "Every Time I Die"; Song[1] = "Kill the Music"; // etc……
What’s wrong with this • Two arrays must be synchronized • Filling the arrays with Artists and Songs is done IN THE CODE. Bad. • One more thing….
Sorting public static void sortByArtist() { for (int y = 0; y<length; y++) { for (int z = 0; z<y; z++) { if ( Artist[z].compareTo(Artist[y]) > 0) { swap( y,z); }// end if } // end for z } // end for y } // end sort by artist
Swap –permanently moves items public static void swap( int a, int b) { String temp; temp = Artist[a]; Artist[a] = Artist[b]; Artist[b] = temp; temp = Song[a]; Song[a] = Song[b]; Song[b] = temp; }
Step 1 – get information from files Put all the database info into a file, rather than in your code.