Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted as Java by MyList ( 15 years ago )
//Andrew Emad
//22-4500
//T-21
public class MyList
{
private String []Names;
public MyList()
{
String[]name;
name=null;
this.Names=name;
}
public MyList(String name)
{
String[]Name;
Name=new String[1];
this.Names=Name;
this.Names[0]=name;
}
public MyList(String[] name)
{
this.Names=name;
String[]newNames=name;
this.Names=newNames;
}
public int getLength()
{
return Names.length;
}
public String getFirst()
{
return Names[0];
}
public String[] getRest()
{
String []Rest=new String[Names.length];
for(int i=0; i<Names.length-1; i++)
{
Rest[i]=Names[i+1];
System.out.print(Rest[i]);
System.out.print(" ");
}
System.out.println ("\n");
return Rest;
}
public void Insert(String givenName, int position)
{
String[]newNames=new String[Names.length+1];
for(int i=0; i<=Names.length; i++)
{
if(i!=position)
newNames[i]=Names[i];
if(i==position)
{
newNames[i]=givenName;
while(i<Names.length)
{
newNames[i+1]=Names[i];
i++;
}
}
}
Names=newNames;
for(int i=0; i<Names.length; i++)
{
System.out.print(Names[i]+" ");
}
System.out.println ("\n");
}
public void Delete(String givenName)
{
int count=0;
int i=0;
while(i<Names.length)
{
if(givenName.equals(Names[i]))
count++;
i++;
}
String[]newNames=new String[Names.length-count];
int j=0;
while(j<newNames.length)
{
if(!givenName.equals(Names[j]))
{
newNames[j]=Names[j];
j++;
}
else
{
newNames[j]=Names[j+1];
j++;
}
}
Names=newNames;
for(i=0; i<Names.length; i++)
{
System.out.print(Names[i]+" ");
}
System.out.println ("\n");
}
public boolean isEmpty()
{
if(Names.length==0)
return true;
else
return false;
}
public boolean subList(String [] givenList)
{
int j=0;
for(int i=0; i<givenList.length; i++){
boolean flag=false;
while(j<Names.length && flag==false){
if(givenList[i].equals(Names[j])){
flag=true;
}
j++;
}
if (flag==false){
return false;
}
}
return true;
}
public static void main(String[] args)
{
String []Test={"Andrew","Peter","John","Peter","Alfred"};
MyList test2=new MyList(Test);
//Test for the program!
System.out.println ("The length of the list is "+test2.getLength()+"\n");
System.out.println ("The first name in the list is "+test2.getFirst()+"\n");
System.out.print ("The rest names in the list are : ");
test2.getRest();
System.out.print ("The list will be after inserting Tom in poisition 2 : ");
test2.Insert("Tom",1);
System.out.print ("The list will be after deleting Peter from the list : ");
test2.Delete("Peter");
System.out.println ("The list "+(test2.isEmpty()? "is " : "isn't ")+"empty.\n");
String[] L1={"Andrew","Alfred"};
System.out.print ("The list : ");
for(int i=0; i<L1.length; i++)
{
System.out.print (L1[i]+" ");
}
System.out.println ((test2.subList(L1)? "is " : "isn't ")+"sub list of the original list.\n");
String []L2={"Andrew","John","Fady","Alfred"};
System.out.print ("The list : ");
for(int i=0; i<L2.length; i++)
{
System.out.print (L2[i]+" ");
}
System.out.println ((test2.subList(L2)? "is " : "isn't ")+"sub list of the original list.\n");
String []L3={"John","Andrew"};
System.out.print ("The list : ");
for(int i=0; i<L3.length; i++)
{
System.out.print (L3[i]+" ");
}
System.out.println ((test2.subList(L3)? "is " : "isn't ")+"sub list of the original list.\n");
}
}
Revise this Paste
Parent: 36382
Children: 36393