Results 1 to 2 of 2
- 10-05-2010, 10:18 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 24
- Rep Power
- 0
Insert String at End of ArrayStringLog
Trying to figure out how to insert a String at the "End" of an Array String Log. Here is the code I have so far, right now the "insert" and "insertAtEnd" are obviously doing the same thing but I'm not sure how to make it do otherwise:
public void insert(String element)
// Precondition: This StringLog is not full.
//
// Places element into this StringLog.
{
if (isFull())
lastIndex++;
else {
lastIndex++;
log[lastIndex] = element;
}
}
public void insertAtEnd(String element)
{
if (isFull())
lastIndex++;
else{
lastIndex++;
log[lastIndex] = element;
}
}
I'm using a class called UseStringLog to insert strings using this method:
log.insert("String 1");
log.insert("String 2");
etc.
and it prints like this:
1. String 1
2. String 2
etc.
How would I change the "insertAtEnd" to get the string to insert before "String 1"?
- 10-05-2010, 10:22 PM #2
Member
- Join Date
- Feb 2010
- Posts
- 24
- Rep Power
- 0
Here is the entire ArrayStringLog class if that helps:
public class ArrayStringLog implements StringLogInterface {
protected String name; // name of this StringLog
protected String[] log; // array that holds strings
protected int lastIndex = -1; // index of last string in array
public ArrayStringLog(String name, int maxSize)
// Precondition: maxSize > 0
//
// Instantiates and returns a reference to an empty StringLog object
// with name "name" and room for maxSize strings.
{
log = new String[maxSize];
this.name = name;
}
public ArrayStringLog(String name)
// Instantiates and returns a reference to an empty StringLog object
// with name "name" and room for 100 strings.
{
log = new String[100];
this.name = name;
}
public void insert(String element)
// Precondition: This StringLog is not full.
//
// Places element into this StringLog.
{
if (isFull())
lastIndex++;
//throw new FullLogException("Full Log\n");
else {
lastIndex++;
log[lastIndex] = element;
}
}
public void insertAtEnd(String element)
{
if (isFull())
lastIndex++;
else{
lastIndex++;
log[lastIndex] = element;
}
}
public boolean isFull()
// Returns true if this StringLog is full, otherwise returns false.
{
if (lastIndex == (log.length - 1))
return true;
else
return false;
}
public int size()
// Returns the number of Strings in this StringLog.
{
return (lastIndex + 1);
}
public boolean contains(String element)
// Returns true if element is in this StringLog,
// otherwise returns false.
// Ignores case differences when doing string comparison.
{
boolean moreToSearch;
int location = 0;
boolean found = false;
moreToSearch = (location <= lastIndex);
while (moreToSearch && !found)
{
if (element.equalsIgnoreCase(log[location])) // if they match
found = true;
else
{
location++;
moreToSearch = (location <= lastIndex);
}
}
return found;
}
public void clear()
// Makes this StringLog empty.
{
for (int i = 0; i <= lastIndex; i++)
log[i] = null;
lastIndex = -1;
}
public String getName()
// Returns the name of this StringLog.
{
return name;
}
public String toString()
// Returns a nicely formatted string representing this StringLog.
{
String logString = "Log: " + name + "\n\n";
for (int i = 0; i <= lastIndex; i++)
logString = logString + (i+1) + ". " + log[i] + "\n";
return logString;
}
}
Similar Threads
-
How can i insert a char into a string
By Jamie in forum New To JavaReplies: 8Last Post: 02-17-2011, 08:59 PM -
insert a row into the DB
By miko5054 in forum JDBCReplies: 13Last Post: 07-11-2010, 02:01 PM -
jsp insert into database error(java.lang.NumberFormatException: For input string: "")
By cypher_girl in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 12-22-2009, 03:14 AM -
How to insert large data into database using one insert query
By sandeepsai39 in forum New To JavaReplies: 3Last Post: 02-28-2009, 09:17 AM -
SQL Insert Help!!!!
By shaungoater in forum New To JavaReplies: 1Last Post: 06-14-2008, 03:14 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks