SlideShare a Scribd company logo
1
ArrayApp
class ArrayApp
{
public static void main(String[] args)
{
long[] arr; // reference to array
arr = new long[100]; // make array
int nElems = 0; // number of items
int j; // loop counter
long searchKey; // key of item to search for
arr[0] = 77; // insert 10 items
arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
arr[7] = 00;
arr[8] = 66;
arr[9] = 33;
nElems = 10; // now 10 items in array
for(j=0; j<nElems; j++) // display items
System.out.print(arr[j] + " ");
System.out.println("");
searchKey = 66; // find item with key 66
for(j=0; j<nElems; j++) // for each element,
if(arr[j] == searchKey) // found item?
break; // yes, exit before end
if(j == nElems) // at the end?
System.out.println("Can't find " + searchKey); // yes
else
System.out.println("Found " + searchKey); // no
searchKey = 55; // delete item with key 55
for(j=0; j<nElems; j++) // look for it
if(arr[j] == searchKey)
break;
for(int k=j; k<nElems; k++) // move higher ones down
arr[k] = arr[k+1];
nElems--; // decrement size
for(j=0; j<nElems; j++) // display items
System.out.print( arr[j] + " ");
System.out.println("");
} // end main()
} // end class ArrayApp
2
Low Array
class LowArray
{
private long[] a; // ref to array a
public LowArray(int size) // constructor
{ a = new long[size]; } // create array
public void setElem(int index, long value) // set value
{ a[index] = value; }
public long getElem(int index) // get value
{ return a[index]; }
} // end class LowArray
class LowArrayApp
{
public static void main(String[] args)
{
LowArray arr; // reference
arr = new LowArray(100); // create LowArray object
int nElems = 0; // number of items in array
int j; // loop variable
arr.setElem(0, 77); // insert 10 items
arr.setElem(1, 99);
arr.setElem(2, 44);
arr.setElem(3, 55);
arr.setElem(4, 22);
arr.setElem(5, 88);
arr.setElem(6, 11);
arr.setElem(7, 00);
arr.setElem(8, 66);
arr.setElem(9, 33);
nElems = 10; // now 10 items in array
for(j=0; j<nElems; j++) // display items
System.out.print(arr.getElem(j) + " ");
System.out.println("");
int searchKey = 26; // search for data item
for(j=0; j<nElems; j++) // for each element,
if(arr.getElem(j) == searchKey) // found item?
break;
if(j == nElems) // no
System.out.println("Can't find " + searchKey);
else // yes
System.out.println("Found " + searchKey);
// delete value 55
3
for(j=0; j<nElems; j++) // look for it
if(arr.getElem(j) == 55)
break;
for(int k=j; k<nElems; k++) // higher ones down
arr.setElem(k, arr.getElem(k+1) );
nElems--; // decrement size
for(j=0; j<nElems; j++) // display items
System.out.print( arr.getElem(j) + " ");
System.out.println("");
} // end main()
} // end class LowArrayApp
4
High Array
class HighArray
{
private long[] a; // ref to array a
private int nElems; // number of data items
public HighArray(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
public boolean find(long searchKey)
{ // find specified value
int j;
for(j=0; j<nElems; j++) // for each element,
if(a[j] == searchKey) // found item?
break; // exit loop before end
if(j == nElems) // gone to end?
return false; // yes, can't find it
else
return true; // no, found it
} // end find()
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
public boolean delete(long value)
{
int j;
for(j=0; j<nElems; j++) // look for it
if( value == a[j] )
break;
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move higher ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
5
System.out.print(a[j] + " "); // display it
System.out.println("");
}
} // end class HighArray
class HighArrayApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
HighArray arr; // reference to array
arr = new HighArray(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); // display items
int searchKey = 35; // search for item
if( arr.find(searchKey) )
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.display(); // display items again
} // end main()
} // end class HighArrayApp
6
Ordered Array
class OrdArray
{
private long[] a; // ref to array a
private int nElems; // number of data items
public OrdArray(int max) // constructor
{
a = new long[max]; // create array
nElems = 0;
}
public int size()
{ return nElems; }
public int find(long searchKey)
{
int lowerBound = 0;
int upperBound = nElems-1;
int curIn;
while(true)
{
curIn = (lowerBound + upperBound ) / 2;
if(a[curIn]==searchKey)
return curIn; // found it
else if(lowerBound > upperBound)
return nElems; // can't find it
else // divide range
{
7
if(a[curIn] < searchKey)
lowerBound = curIn + 1; // it's in upper half
else
upperBound = curIn - 1; // it's in lower half
} // end else divide range
} // end while
} // end find()
public void insert(long value) // put element into array
{
int j;
for(j=0; j<nElems; j++) // find where it goes
if(a[j] > value) // (linear search)
break;
for(int k=nElems; k>j; k--) // move bigger ones up
a[k] = a[k-1];
a[j] = value; // insert it
nElems++; // increment size
} // end insert()
public boolean delete(long value)
{
int j = find(value);
if(j==nElems) // can't find it
return false;
else // found it
{
for(int k=j; k<nElems; k++) // move bigger ones down
a[k] = a[k+1];
nElems--; // decrement size
return true;
8
}
} // end delete()
public void display() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
} // end class OrdArray
class OrderedArrayApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
OrdArray arr; // reference to array
arr = new OrdArray(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
9
int searchKey = 55; // search for item
if( arr.find(searchKey) != arr.size() )
System.out.println("Found " + searchKey);
else
System.out.println("Can't find " + searchKey);
arr.display(); // display items
arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.display(); // display items again
} // end main()
} // end class OrderedApp
10
Class Data Array
class Person
{
private String lastName;
private String firstName;
private int age;
public Person(String last, String first, int a)
{ // constructor
lastName = last;
firstName = first;
age = a;
}
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Age: " + age);
}
public String getLast() // get last name
{ return lastName; }
} // end class Person
class ClassDataArray
{
private Person[] a; // reference to array
private int nElems; // number of data items
11
public ClassDataArray(int max) // constructor
{
a = new Person[max]; // create the array
nElems = 0; // no items yet
}
public Person find(String searchName)
{ // find specified value
int j;
for(j=0; j<nElems; j++) // for each element,
if( a[j].getLast().equals(searchName) ) // found item?
break; // exit loop before end
if(j == nElems) // gone to end?
return null; // yes, can't find it
else
return a[j]; // no, found it
} // end find()
public void insert(String last, String first, int age)
{
a[nElems] = new Person(last, first, age);
nElems++; // increment size
}
public boolean delete(String searchName)
{ // delete person from array
int j;
for(j=0; j<nElems; j++) // look for it
if( a[j].getLast().equals(searchName) )
break;
if(j==nElems) // can't find it
12
return false;
else // found it
{
for(int k=j; k<nElems; k++) // shift down
a[k] = a[k+1];
nElems--; // decrement size
return true;
}
} // end delete()
public void displayA() // displays array contents
{
for(int j=0; j<nElems; j++) // for each element,
a[j].displayPerson(); // display it
}
} // end class ClassDataArray
class ClassDataApp
{
public static void main(String[] args)
{
int maxSize = 100; // array size
ClassDataArray arr; // reference to array
arr = new ClassDataArray(maxSize); // create the array
// insert 10 items
arr.insert("Evans", "Patty", 24);
arr.insert("Smith", "Lorraine", 37);
arr.insert("Yee", "Tom", 43);
arr.insert("Adams", "Henry", 63);
arr.insert("Hashimoto", "Sato", 21);
13
arr.insert("Stimson", "Henry", 29);
arr.insert("Velasquez", "Jose", 72);
arr.insert("Lamarque", "Henry", 54);
arr.insert("Vang", "Minh", 22);
arr.insert("Creswell", "Lucinda", 18);
arr.displayA(); // display items
String searchKey = "Stimson"; // search for item
Person found;
found=arr.find(searchKey);
if(found != null)
{
System.out.print("Found ");
found.displayPerson();
}
else
System.out.println("Can't find " + searchKey);
System.out.println("Deleting Smith, Yee, and Creswell");
arr.delete("Smith"); // delete 3 items
arr.delete("Yee");
arr.delete("Creswell");
arr.displayA(); // display items again
} // end main()
} // end class ClassDataApp
Ad

Recommended

Aller plus loin avec Doctrine2
Aller plus loin avec Doctrine2
André Tapia
 
Danna y felix 10°
Danna y felix 10°
danna gabriela
 
Programming Java - Lection 03 - Classes - Lavrentyev Fedor
Programming Java - Lection 03 - Classes - Lavrentyev Fedor
Fedor Lavrentyev
 
EJEMPLOS DESARROLLADOS
EJEMPLOS DESARROLLADOS
Darwin Durand
 
Java AWT Calculadora
Java AWT Calculadora
jubacalo
 
Assalamualaykum warahmatullahi wabarakatuu
Assalamualaykum warahmatullahi wabarakatuu
iswan_di
 
Java
Java
Antonio Furone
 
Шаблоны проектирования 2
Шаблоны проектирования 2
Constantin Kichinsky
 
es6.concurrency()
es6.concurrency()
Ingvar Stepanyan
 
Proyecto Final Android-SQLite
Proyecto Final Android-SQLite
José Antonio Sandoval Acosta
 
Java весна 2013 лекция 7
Java весна 2013 лекция 7
Technopark
 
Java Thread Cronometro
Java Thread Cronometro
jubacalo
 
Hacer una calculadora en Java y en Visual Basic
Hacer una calculadora en Java y en Visual Basic
HumbertoWuwu
 
JQuery
JQuery
koji lin
 
Propuesta..sujei
Propuesta..sujei
gersonjack
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
Anton Arhipov
 
Sis quiz
Sis quiz
Clesio Veloso
 
Import java
Import java
wildled
 
Testování prakticky
Testování prakticky
Filip Procházka
 
Java весна 2013 лекция 6
Java весна 2013 лекция 6
Technopark
 
JavaScript Avanzado
JavaScript Avanzado
Adolfo Sanz De Diego
 
Semana 12 interfaces gráficas de usuario
Semana 12 interfaces gráficas de usuario
TerryJoss
 
Jak zabít několik much jednou ranou přechodem na fragmenty
Jak zabít několik much jednou ranou přechodem na fragmenty
David Vávra
 
聞いてスッキリ!Lightningの理解ポイント
聞いてスッキリ!Lightningの理解ポイント
寛 吉田
 
Practical JavaScript Programming - Session 3/8
Practical JavaScript Programming - Session 3/8
Wilson Su
 
Binari searc
Binari searc
ayi_ayi
 
Pengembangan Basis Data untuk Web Application.pptx
Pengembangan Basis Data untuk Web Application.pptx
Fajar Baskoro
 
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Fajar Baskoro
 

More Related Content

What's hot (20)

es6.concurrency()
es6.concurrency()
Ingvar Stepanyan
 
Proyecto Final Android-SQLite
Proyecto Final Android-SQLite
José Antonio Sandoval Acosta
 
Java весна 2013 лекция 7
Java весна 2013 лекция 7
Technopark
 
Java Thread Cronometro
Java Thread Cronometro
jubacalo
 
Hacer una calculadora en Java y en Visual Basic
Hacer una calculadora en Java y en Visual Basic
HumbertoWuwu
 
JQuery
JQuery
koji lin
 
Propuesta..sujei
Propuesta..sujei
gersonjack
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
Anton Arhipov
 
Sis quiz
Sis quiz
Clesio Veloso
 
Import java
Import java
wildled
 
Testování prakticky
Testování prakticky
Filip Procházka
 
Java весна 2013 лекция 6
Java весна 2013 лекция 6
Technopark
 
JavaScript Avanzado
JavaScript Avanzado
Adolfo Sanz De Diego
 
Semana 12 interfaces gráficas de usuario
Semana 12 interfaces gráficas de usuario
TerryJoss
 
Jak zabít několik much jednou ranou přechodem na fragmenty
Jak zabít několik much jednou ranou přechodem na fragmenty
David Vávra
 
聞いてスッキリ!Lightningの理解ポイント
聞いてスッキリ!Lightningの理解ポイント
寛 吉田
 
Practical JavaScript Programming - Session 3/8
Practical JavaScript Programming - Session 3/8
Wilson Su
 
Binari searc
Binari searc
ayi_ayi
 
Java весна 2013 лекция 7
Java весна 2013 лекция 7
Technopark
 
Java Thread Cronometro
Java Thread Cronometro
jubacalo
 
Hacer una calculadora en Java y en Visual Basic
Hacer una calculadora en Java y en Visual Basic
HumbertoWuwu
 
Propuesta..sujei
Propuesta..sujei
gersonjack
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
Anton Arhipov
 
Import java
Import java
wildled
 
Java весна 2013 лекция 6
Java весна 2013 лекция 6
Technopark
 
Semana 12 interfaces gráficas de usuario
Semana 12 interfaces gráficas de usuario
TerryJoss
 
Jak zabít několik much jednou ranou přechodem na fragmenty
Jak zabít několik much jednou ranou přechodem na fragmenty
David Vávra
 
聞いてスッキリ!Lightningの理解ポイント
聞いてスッキリ!Lightningの理解ポイント
寛 吉田
 
Practical JavaScript Programming - Session 3/8
Practical JavaScript Programming - Session 3/8
Wilson Su
 
Binari searc
Binari searc
ayi_ayi
 

More from Fajar Baskoro (20)

Pengembangan Basis Data untuk Web Application.pptx
Pengembangan Basis Data untuk Web Application.pptx
Fajar Baskoro
 
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Fajar Baskoro
 
Sosialisasi Program Digital Skills Unicef 2025.pptx
Sosialisasi Program Digital Skills Unicef 2025.pptx
Fajar Baskoro
 
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
Fajar Baskoro
 
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
Fajar Baskoro
 
Pemrograman Mobile menggunakan kotlin2.pdf
Pemrograman Mobile menggunakan kotlin2.pdf
Fajar Baskoro
 
Membangun Kewirausahan Sosial Program Double Track.pptx
Membangun Kewirausahan Sosial Program Double Track.pptx
Fajar Baskoro
 
Membangun Kemandirian DTMandiri-2025.pptx
Membangun Kemandirian DTMandiri-2025.pptx
Fajar Baskoro
 
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
Fajar Baskoro
 
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
Fajar Baskoro
 
Seleksi Penerimaan Murid Baru 2025.pptx
Seleksi Penerimaan Murid Baru 2025.pptx
Fajar Baskoro
 
Pengembangan Program Dual Track 2025-2.pptx
Pengembangan Program Dual Track 2025-2.pptx
Fajar Baskoro
 
Pengembangan Program Dual Track 2025-1.pptx
Pengembangan Program Dual Track 2025-1.pptx
Fajar Baskoro
 
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
Fajar Baskoro
 
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
Fajar Baskoro
 
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
Fajar Baskoro
 
Program Dual Track Kalimantan Timur 2025.pptx
Program Dual Track Kalimantan Timur 2025.pptx
Fajar Baskoro
 
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
Fajar Baskoro
 
Pengembangan Program Digital Skills - 2025.pptx
Pengembangan Program Digital Skills - 2025.pptx
Fajar Baskoro
 
PPT-Proyek Magang Kewirausahaan Double Track.pptx
PPT-Proyek Magang Kewirausahaan Double Track.pptx
Fajar Baskoro
 
Pengembangan Basis Data untuk Web Application.pptx
Pengembangan Basis Data untuk Web Application.pptx
Fajar Baskoro
 
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Presentasi untuk video Pitch Deck Vlog Pervekt SMK 2025.pptx
Fajar Baskoro
 
Sosialisasi Program Digital Skills Unicef 2025.pptx
Sosialisasi Program Digital Skills Unicef 2025.pptx
Fajar Baskoro
 
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
DIGITAL SKILLS PROGRAMME 2025 - VERSI HZ.pdf
Fajar Baskoro
 
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
Digital Skills - 2025 - Dinas - Green Marketplace.pdf
Fajar Baskoro
 
Pemrograman Mobile menggunakan kotlin2.pdf
Pemrograman Mobile menggunakan kotlin2.pdf
Fajar Baskoro
 
Membangun Kewirausahan Sosial Program Double Track.pptx
Membangun Kewirausahan Sosial Program Double Track.pptx
Fajar Baskoro
 
Membangun Kemandirian DTMandiri-2025.pptx
Membangun Kemandirian DTMandiri-2025.pptx
Fajar Baskoro
 
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
Panduan Entry Nilai Rapor untuk Operator SD_MI 2025.pptx (1).pdf
Fajar Baskoro
 
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
JADWAL SISTEM PENERIMAAN MURID BARU 2025.pdf
Fajar Baskoro
 
Seleksi Penerimaan Murid Baru 2025.pptx
Seleksi Penerimaan Murid Baru 2025.pptx
Fajar Baskoro
 
Pengembangan Program Dual Track 2025-2.pptx
Pengembangan Program Dual Track 2025-2.pptx
Fajar Baskoro
 
Pengembangan Program Dual Track 2025-1.pptx
Pengembangan Program Dual Track 2025-1.pptx
Fajar Baskoro
 
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
PETUNJUK PELAKSANAAN TEKNIS FESV RAMADHAN 2025.pdf
Fajar Baskoro
 
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
Pengembangan Entrepreneur Vokasi Melalui PERFECT SMK-Society 50 .pptx
Fajar Baskoro
 
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
PERFECT SMK 6 - Strategi Pelaksanaan.pptx
Fajar Baskoro
 
Program Dual Track Kalimantan Timur 2025.pptx
Program Dual Track Kalimantan Timur 2025.pptx
Fajar Baskoro
 
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
Contoh Proposal konveksi untuk Program Magang Kewirausahaan.pdf
Fajar Baskoro
 
Pengembangan Program Digital Skills - 2025.pptx
Pengembangan Program Digital Skills - 2025.pptx
Fajar Baskoro
 
PPT-Proyek Magang Kewirausahaan Double Track.pptx
PPT-Proyek Magang Kewirausahaan Double Track.pptx
Fajar Baskoro
 
Ad

Recently uploaded (10)

cambios_emocionales en los traumas presentes en adolescentes
cambios_emocionales en los traumas presentes en adolescentes
vivianyarenivallecil
 
Aylanmadan olinadigan soliq.2025 yil.pdf
Aylanmadan olinadigan soliq.2025 yil.pdf
ilxomislomov2020
 
Flashcards Animais brasileiros Ilustrado Verde.pdf
Flashcards Animais brasileiros Ilustrado Verde.pdf
PriscilaRibeiro803210
 
Gazetteer of Russia. Part: Populated places A-F.pdf
Gazetteer of Russia. Part: Populated places A-F.pdf
peivhau
 
Reportàge on Kaliningrad LEMONDE.3PAG.pdf
Reportàge on Kaliningrad LEMONDE.3PAG.pdf
Stefano Lariccia
 
15 June 2025 PS - 15 June 2025 PS - 15 June 2025 PS -
15 June 2025 PS - 15 June 2025 PS - 15 June 2025 PS -
ssuser787edf
 
наказ про зарахуваннядо 1 класу kg72 2025
наказ про зарахуваннядо 1 класу kg72 2025
AleksSaf
 
History for the class 12 students and class 10
History for the class 12 students and class 10
suseelasudarmani
 
Gazetteer of Russia. Part: Populated places G-Krasno.pdf
Gazetteer of Russia. Part: Populated places G-Krasno.pdf
peivhau
 
pdf-p-classtruncatedtext-module-lineclamped-85ulhh-style-max-lines5topik-tema...
pdf-p-classtruncatedtext-module-lineclamped-85ulhh-style-max-lines5topik-tema...
Ifa Nofalia
 
cambios_emocionales en los traumas presentes en adolescentes
cambios_emocionales en los traumas presentes en adolescentes
vivianyarenivallecil
 
Aylanmadan olinadigan soliq.2025 yil.pdf
Aylanmadan olinadigan soliq.2025 yil.pdf
ilxomislomov2020
 
Flashcards Animais brasileiros Ilustrado Verde.pdf
Flashcards Animais brasileiros Ilustrado Verde.pdf
PriscilaRibeiro803210
 
Gazetteer of Russia. Part: Populated places A-F.pdf
Gazetteer of Russia. Part: Populated places A-F.pdf
peivhau
 
Reportàge on Kaliningrad LEMONDE.3PAG.pdf
Reportàge on Kaliningrad LEMONDE.3PAG.pdf
Stefano Lariccia
 
15 June 2025 PS - 15 June 2025 PS - 15 June 2025 PS -
15 June 2025 PS - 15 June 2025 PS - 15 June 2025 PS -
ssuser787edf
 
наказ про зарахуваннядо 1 класу kg72 2025
наказ про зарахуваннядо 1 класу kg72 2025
AleksSaf
 
History for the class 12 students and class 10
History for the class 12 students and class 10
suseelasudarmani
 
Gazetteer of Russia. Part: Populated places G-Krasno.pdf
Gazetteer of Russia. Part: Populated places G-Krasno.pdf
peivhau
 
pdf-p-classtruncatedtext-module-lineclamped-85ulhh-style-max-lines5topik-tema...
pdf-p-classtruncatedtext-module-lineclamped-85ulhh-style-max-lines5topik-tema...
Ifa Nofalia
 
Ad

1- Sourcecode Array

  • 1. 1 ArrayApp class ArrayApp { public static void main(String[] args) { long[] arr; // reference to array arr = new long[100]; // make array int nElems = 0; // number of items int j; // loop counter long searchKey; // key of item to search for arr[0] = 77; // insert 10 items arr[1] = 99; arr[2] = 44; arr[3] = 55; arr[4] = 22; arr[5] = 88; arr[6] = 11; arr[7] = 00; arr[8] = 66; arr[9] = 33; nElems = 10; // now 10 items in array for(j=0; j<nElems; j++) // display items System.out.print(arr[j] + " "); System.out.println(""); searchKey = 66; // find item with key 66 for(j=0; j<nElems; j++) // for each element, if(arr[j] == searchKey) // found item? break; // yes, exit before end if(j == nElems) // at the end? System.out.println("Can't find " + searchKey); // yes else System.out.println("Found " + searchKey); // no searchKey = 55; // delete item with key 55 for(j=0; j<nElems; j++) // look for it if(arr[j] == searchKey) break; for(int k=j; k<nElems; k++) // move higher ones down arr[k] = arr[k+1]; nElems--; // decrement size for(j=0; j<nElems; j++) // display items System.out.print( arr[j] + " "); System.out.println(""); } // end main() } // end class ArrayApp
  • 2. 2 Low Array class LowArray { private long[] a; // ref to array a public LowArray(int size) // constructor { a = new long[size]; } // create array public void setElem(int index, long value) // set value { a[index] = value; } public long getElem(int index) // get value { return a[index]; } } // end class LowArray class LowArrayApp { public static void main(String[] args) { LowArray arr; // reference arr = new LowArray(100); // create LowArray object int nElems = 0; // number of items in array int j; // loop variable arr.setElem(0, 77); // insert 10 items arr.setElem(1, 99); arr.setElem(2, 44); arr.setElem(3, 55); arr.setElem(4, 22); arr.setElem(5, 88); arr.setElem(6, 11); arr.setElem(7, 00); arr.setElem(8, 66); arr.setElem(9, 33); nElems = 10; // now 10 items in array for(j=0; j<nElems; j++) // display items System.out.print(arr.getElem(j) + " "); System.out.println(""); int searchKey = 26; // search for data item for(j=0; j<nElems; j++) // for each element, if(arr.getElem(j) == searchKey) // found item? break; if(j == nElems) // no System.out.println("Can't find " + searchKey); else // yes System.out.println("Found " + searchKey); // delete value 55
  • 3. 3 for(j=0; j<nElems; j++) // look for it if(arr.getElem(j) == 55) break; for(int k=j; k<nElems; k++) // higher ones down arr.setElem(k, arr.getElem(k+1) ); nElems--; // decrement size for(j=0; j<nElems; j++) // display items System.out.print( arr.getElem(j) + " "); System.out.println(""); } // end main() } // end class LowArrayApp
  • 4. 4 High Array class HighArray { private long[] a; // ref to array a private int nElems; // number of data items public HighArray(int max) // constructor { a = new long[max]; // create the array nElems = 0; // no items yet } public boolean find(long searchKey) { // find specified value int j; for(j=0; j<nElems; j++) // for each element, if(a[j] == searchKey) // found item? break; // exit loop before end if(j == nElems) // gone to end? return false; // yes, can't find it else return true; // no, found it } // end find() public void insert(long value) // put element into array { a[nElems] = value; // insert it nElems++; // increment size } public boolean delete(long value) { int j; for(j=0; j<nElems; j++) // look for it if( value == a[j] ) break; if(j==nElems) // can't find it return false; else // found it { for(int k=j; k<nElems; k++) // move higher ones down a[k] = a[k+1]; nElems--; // decrement size return true; } } // end delete() public void display() // displays array contents { for(int j=0; j<nElems; j++) // for each element,
  • 5. 5 System.out.print(a[j] + " "); // display it System.out.println(""); } } // end class HighArray class HighArrayApp { public static void main(String[] args) { int maxSize = 100; // array size HighArray arr; // reference to array arr = new HighArray(maxSize); // create the array arr.insert(77); // insert 10 items arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33); arr.display(); // display items int searchKey = 35; // search for item if( arr.find(searchKey) ) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); arr.delete(00); // delete 3 items arr.delete(55); arr.delete(99); arr.display(); // display items again } // end main() } // end class HighArrayApp
  • 6. 6 Ordered Array class OrdArray { private long[] a; // ref to array a private int nElems; // number of data items public OrdArray(int max) // constructor { a = new long[max]; // create array nElems = 0; } public int size() { return nElems; } public int find(long searchKey) { int lowerBound = 0; int upperBound = nElems-1; int curIn; while(true) { curIn = (lowerBound + upperBound ) / 2; if(a[curIn]==searchKey) return curIn; // found it else if(lowerBound > upperBound) return nElems; // can't find it else // divide range {
  • 7. 7 if(a[curIn] < searchKey) lowerBound = curIn + 1; // it's in upper half else upperBound = curIn - 1; // it's in lower half } // end else divide range } // end while } // end find() public void insert(long value) // put element into array { int j; for(j=0; j<nElems; j++) // find where it goes if(a[j] > value) // (linear search) break; for(int k=nElems; k>j; k--) // move bigger ones up a[k] = a[k-1]; a[j] = value; // insert it nElems++; // increment size } // end insert() public boolean delete(long value) { int j = find(value); if(j==nElems) // can't find it return false; else // found it { for(int k=j; k<nElems; k++) // move bigger ones down a[k] = a[k+1]; nElems--; // decrement size return true;
  • 8. 8 } } // end delete() public void display() // displays array contents { for(int j=0; j<nElems; j++) // for each element, System.out.print(a[j] + " "); // display it System.out.println(""); } } // end class OrdArray class OrderedArrayApp { public static void main(String[] args) { int maxSize = 100; // array size OrdArray arr; // reference to array arr = new OrdArray(maxSize); // create the array arr.insert(77); // insert 10 items arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33);
  • 9. 9 int searchKey = 55; // search for item if( arr.find(searchKey) != arr.size() ) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); arr.display(); // display items arr.delete(00); // delete 3 items arr.delete(55); arr.delete(99); arr.display(); // display items again } // end main() } // end class OrderedApp
  • 10. 10 Class Data Array class Person { private String lastName; private String firstName; private int age; public Person(String last, String first, int a) { // constructor lastName = last; firstName = first; age = a; } public void displayPerson() { System.out.print(" Last name: " + lastName); System.out.print(", First name: " + firstName); System.out.println(", Age: " + age); } public String getLast() // get last name { return lastName; } } // end class Person class ClassDataArray { private Person[] a; // reference to array private int nElems; // number of data items
  • 11. 11 public ClassDataArray(int max) // constructor { a = new Person[max]; // create the array nElems = 0; // no items yet } public Person find(String searchName) { // find specified value int j; for(j=0; j<nElems; j++) // for each element, if( a[j].getLast().equals(searchName) ) // found item? break; // exit loop before end if(j == nElems) // gone to end? return null; // yes, can't find it else return a[j]; // no, found it } // end find() public void insert(String last, String first, int age) { a[nElems] = new Person(last, first, age); nElems++; // increment size } public boolean delete(String searchName) { // delete person from array int j; for(j=0; j<nElems; j++) // look for it if( a[j].getLast().equals(searchName) ) break; if(j==nElems) // can't find it
  • 12. 12 return false; else // found it { for(int k=j; k<nElems; k++) // shift down a[k] = a[k+1]; nElems--; // decrement size return true; } } // end delete() public void displayA() // displays array contents { for(int j=0; j<nElems; j++) // for each element, a[j].displayPerson(); // display it } } // end class ClassDataArray class ClassDataApp { public static void main(String[] args) { int maxSize = 100; // array size ClassDataArray arr; // reference to array arr = new ClassDataArray(maxSize); // create the array // insert 10 items arr.insert("Evans", "Patty", 24); arr.insert("Smith", "Lorraine", 37); arr.insert("Yee", "Tom", 43); arr.insert("Adams", "Henry", 63); arr.insert("Hashimoto", "Sato", 21);
  • 13. 13 arr.insert("Stimson", "Henry", 29); arr.insert("Velasquez", "Jose", 72); arr.insert("Lamarque", "Henry", 54); arr.insert("Vang", "Minh", 22); arr.insert("Creswell", "Lucinda", 18); arr.displayA(); // display items String searchKey = "Stimson"; // search for item Person found; found=arr.find(searchKey); if(found != null) { System.out.print("Found "); found.displayPerson(); } else System.out.println("Can't find " + searchKey); System.out.println("Deleting Smith, Yee, and Creswell"); arr.delete("Smith"); // delete 3 items arr.delete("Yee"); arr.delete("Creswell"); arr.displayA(); // display items again } // end main() } // end class ClassDataApp