Wikipedia

Search results

Monday, 21 October 2013

Arrays

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.
Illustration of an array as 10 boxes numbered 0 through 9; an index of 0 indicates the first element in the array
An array of 10 elements.



Arrays are basically memory location ,it will store the multiple Data,it has the index,

*Some drawbacks of Array 
1)It is fot particular DataTypes
2)Size of the Array

int[i]=new int[10]//Advantage of the Array is Length of the Array

 Object Array :Object array is a inbuilt class it is super class of the class
It can Store Different type of DataTypes
Object ar[] = new Object[4];
ar[0] = "Hello"
ar[1]="1231" 
ar[2]="true"
ar[3]="32.21"

Simple Programm for Array

public class TestArray {

   public static void main(String[] args) {
      double[] myList = {1.9, 2.9, 3.4, 3.5};

      // Print all the array elements
      for (int i = 0; i < myList.length; i++) {
         System.out.println(myList[i] + " ");
      }
      // Summing all elements
      double total = 0;
      for (int i = 0; i < myList.length; i++) {
         total += myList[i];
      }
      System.out.println("Total is " + total);
      // Finding the largest element
      double max = myList[0];
      for (int i = 1; i < myList.length; i++) {
         if (myList[i] > max) max = myList[i];
      }
      System.out.println("Max is " + max);
   }
}



Java String Class




Class java.lang.String

java.lang.Object
   |

public final class String
extends Object
A general class of objects to represent character Strings. Strings are constant, their values cannot be changed after creation. The compiler makes sure that each String constant actually results in a String object. Because String objects are immutable they can be shared. For example:
 String str = "Java";
is equivalent to:
 char data[] = {'j', 'a', 'v' , 'a'};
 String str = new String(data);
Here are some more examples of how strings can be used:
 System.out.println("java");
 String cde = "avaj";
 System.out.println("java" + avaj);
 String c = "java".substring(2,3);
 String d = avaj.substring(1, 2);


Constructors:



 o String
  public String()
Constructs a new empty String.
 o String
  public String(String value)
Constructs a new String that is a copy of the specified String.
Parameters:
value - the initial value of the String
 o String
  public String(char value[])
Constructs a new String whose initial value is the specified array of characters. The character array is NOT copied, so DO NOT modify the array after the String is created!
Parameters:
value - the initial value of the String
 o String
  public String(char value[],
                int offset,
                int count)
Constructs a new String whose initial value is the specified sub array of characters. The length of the new string will be count characters starting at offset within the specified character array. The character array is NOT copied, so DO NOT modify the array after the String is created!
Parameters:
value - the initial value of the String, an array of characters
offset - the offset into the value of the String
count - the length of the value of the String
Throws: StringIndexOutOfBoundsException
If the offset and count arguments are invalid.
 o String
  public String(byte ascii[],
                int hibyte,
                int offset,
                int count)
Constructs a new String whose initial value is the specified sub array of bytes. The high-byte of each character can be specified, it should usually be 0. The length of the new String will be count characters starting at offset within the specified character array.
Parameters:
ascii - the bytes that will be converted to characters
hibyte - the high byte of each Unicode character
offset - the offset into the ascii array
count - the length of the String
Throws: StringIndexOutOfBoundsException
If the offset and count arguments are invalid.
 o String
  public String(byte ascii[],
                int hibyte)
Constructs a new String whose initial value is the specified array of bytes. The byte array transformed into Unicode chars using hibyte as the upper byte of each character.
Parameters:
ascii - the byte that will be converted to characters
hibyte - the top 8 bits of each 16 bit Unicode character

Methods:

 o length
  public int length()
Returns the length of the String. The length of the String is equal to the number of 16 bit Unicode characters in the String.
 o charAt
  public char charAt(int index)
Returns the character at the specified index. An index ranges from 0 to length() - 1.
Parameters:
index - the index of the desired character
Throws: StringIndexOutOfBoundsException
If the index is not in the range 0 to length()-1.
 o getChars
  public void getChars(int srcBegin,
                       int srcEnd,
                       char dst[],
                       int dstBegin)
Copies characters from this String into the specified character array. The characters of the specified substring (determined by srcBegin and srcEnd) are copied into the character array, starting at the array's dstBegin location.
Parameters:
srcBegin - index of the first character in the string
srcEnd - end of the characters that are copied
dst - the destination array
dstBegin - the start offset in the destination array
 o getBytes
  public void getBytes(int srcBegin,
                       int srcEnd,
                       byte dst[],
                       int dstBegin)
Copies characters from this String into the specified byte array. Copies the characters of the specified substring (determined by srcBegin and srcEnd) into the byte array, starting at the array's dstBegin location.
Parameters:
srcBegin - index of the first character in the String
srcEnd - end of the characters that are copied
dst - the destination array
dstBegin - the start offset in the destination array
 o equals
  public boolean equals(Object anObject)
Compares this String to the specified object. Returns true if the object is equal to this String; that is, has the same length and the same characters in the same sequence.
Parameters:
anObject - the object to compare this String against
Returns:
true if the Strings are equal; false otherwise.
Overrides:
equals in class Object
 o equalsIgnoreCase
  public boolean equalsIgnoreCase(String anotherString)
Compares this String to another object. Returns true if the object is equal to this String; that is, has the same length and the same characters in the same sequence. Upper case characters are folded to lower case before they are compared.
Parameters:
anotherString - the String to compare this String against
Returns:
true if the Strings are equal, ignoring case; false otherwise.
 o compareTo
  public int compareTo(String anotherString)
Compares this String to another specified String. Returns an integer that is less than, equal to, or greater than zero. The integer's value depends on whether this String is less than, equal to, or greater than anotherString.
Parameters:
anotherString - the String to be compared
 o regionMatches
  public boolean regionMatches(int toffset,
                               String other,
                               int ooffset,
                               int len)
Determines whether a region of this String matches the specified region of the specified String.
Parameters:
toffset - where to start looking in this String
other - the other String
ooffset - where to start looking in the other String
len - the number of characters to compare
Returns:
true if the region matches with the other; false otherwise.
 o regionMatches
  public boolean regionMatches(boolean ignoreCase,
                               int toffset,
                               String other,
                               int ooffset,
                               int len)
Determines whether a region of this String matches the specified region of the specified String. If the boolean ignoreCase is true, upper case characters are considered equivalent to lower case letters.
Parameters:
ignoreCase - if true, case is ignored
toffset - where to start looking in this String
other - the other String
ooffset - where to start looking in the other String
len - the number of characters to compare
Returns:
true if the region matches with the other; false otherwise.
 o startsWith
  public boolean startsWith(String prefix,
                            int toffset)
Determines whether this String starts with some prefix.
Parameters:
prefix - the prefix
toffset - where to begin looking in the the String
Returns:
true if the String starts with the specified prefix; false otherwise.
 o startsWith
  public boolean startsWith(String prefix)
Determines whether this String starts with some prefix.
Parameters:
prefix - the prefix
Returns:
true if the String starts with the specified prefix; false otherwise.
 o endsWith
  public boolean endsWith(String suffix)
Determines whether the String ends with some suffix.
Parameters:
suffix - the suffix
Returns:
true if the String ends with the specified suffix; false otherwise.
 o hashCode
  public int hashCode()
Returns a hashcode for this String. This is a large number composed of the character values in the String.
Overrides:
hashCode in class Object
 o indexOf
  public int indexOf(int ch)
Returns the index within this String of the first occurrence of the specified character. This method returns -1 if the index is not found.
Parameters:
ch - the character to search for
 o indexOf
  public int indexOf(int ch,
                     int fromIndex)
Returns the index within this String of the first occurrence of the specified character, starting the search at fromIndex. This method returns -1 if the index is not found.
Parameters:
ch - the character to search for
fromIndex - the index to start the search from
 o lastIndexOf
  public int lastIndexOf(int ch)
Returns the index within this String of the last occurrence of the specified character. The String is searched backwards starting at the last character. This method returns -1 if the index is not found.
Parameters:
ch - the character to search for
 o lastIndexOf
  public int lastIndexOf(int ch,
                         int fromIndex)
Returns the index within this String of the last occurrence of the specified character. The String is searched backwards starting at fromIndex. This method returns -1 if the index is not found.
Parameters:
ch - the character to search for
fromIndex - the index to start the search from
 o indexOf
  public int indexOf(String str)
Returns the index within this String of the first occurrence of the specified substring. This method returns -1 if the index is not found.
Parameters:
str - the substring to search for
 o indexOf
  public int indexOf(String str,
                     int fromIndex)
Returns the index within this String of the first occurrence of the specified substring. The search is started at fromIndex. This method returns -1 if the index is not found.
Parameters:
str - the substring to search for
fromIndex - the index to start the search from
 o lastIndexOf
  public int lastIndexOf(String str)
Returns the index within this String of the last occurrence of the specified substring. The String is searched backwards. This method returns -1 if the index is not found.
Parameters:
str - the substring to search for
 o lastIndexOf
  public int lastIndexOf(String str,
                         int fromIndex)
Returns the index within this String of the last occurrence of the specified substring. The String is searched backwards starting at fromIndex. This method returns -1 if the index is not found.
Parameters:
str - the substring to search for
fromIndex - the index to start the search from
 o substring
  public String substring(int beginIndex)
Returns the substring of this String. The substring is specified by a beginIndex (inclusive) and the end of the string.
Parameters:
beginIndex - the beginning index, inclusive
 o substring
  public String substring(int beginIndex,
                          int endIndex)
Returns the substring of a String. The substring is specified by a beginIndex (inclusive) and an endIndex (exclusive).
Parameters:
beginIndex - the beginning index, inclusive
endIndex - the ending index, exclusive
Throws: StringIndexOutOfBoundsException
If the beginIndex or the endIndex is out of range.
 o concat
  public String concat(String str)
Concatenates the specified string to the end of this String.
Parameters:
str - the String which is concatenated to the end of this String
 o replace
  public String replace(char oldChar,
                        char newChar)
Converts this String by replacing all occurences of oldChar with newChar.
Parameters:
oldChar - the old character
newChar - the new character
 o toLowerCase
  public String toLowerCase()
Converts all of the characters in this String to lower case.
Returns:
the String, converted to lowercase.
See Also:
toLowerCase, toUpperCase
 o toUpperCase
  public String toUpperCase()
Converts all of the characters in this String to upper case.
Returns:
the String, converted to uppercase.
See Also:
toUpperCase, toLowerCase
 o trim
  public String trim()
Trims leading and trailing whitespace from this String.
Returns:
the String, with whitespace removed.
 o toString
  public String toString()
Converts this String to a String.
Returns:
the String itself.
Overrides:
toString in class Object
 o toCharArray
  public char[] toCharArray()
Converts this String to a character array. This creates a new array.
Returns:
an array of characters.
 o valueOf
  public static String valueOf(Object obj)
Returns a String that represents the String value of the object. The object may choose how to represent itself by implementing the toString() method.
Parameters:
obj - the object to be converted
 o valueOf
  public static String valueOf(char data[])
Returns a String that is equivalent to the specified character array. Uses the original array as the body of the String (ie. it does not copy it to a new array).
Parameters:
data - the character array
 o valueOf
  public static String valueOf(char data[],
                               int offset,
                               int count)
Returns a String that is equivalent to the specified character array. Uses the original array as the body of the String (ie. it does not copy it to a new array).
Parameters:
data - the character array
offset - the offset into the value of the String
count - the length of the value of the String
 o copyValueOf
  public static String copyValueOf(char data[],
                                   int offset,
                                   int count)
Returns a String that is equivalent to the specified character array. It creates a new array and copies the characters into it.
Parameters:
data - the character array
offset - the offset into the value of the String
count - the length of the value of the String
 o copyValueOf
  public static String copyValueOf(char data[])
Returns a String that is equivalent to the specified character array. It creates a new array and copies the characters into it.
Parameters:
data - the character array
 o valueOf
  public static String valueOf(boolean b)
Returns a String object that represents the state of the specified boolean.
Parameters:
b - the boolean
 o valueOf
  public static String valueOf(char c)
Returns a String object that contains a single character
Parameters:
c - the character
Returns:
the resulting String.
 o valueOf
  public static String valueOf(int i)
Returns a String object that represents the value of the specified integer.
Parameters:
i - the integer
 o valueOf
  public static String valueOf(long l)
Returns a String object that represents the value of the specified long.
Parameters:
l - the long
 o valueOf
  public static String valueOf(float f)
Returns a String object that represents the value of the specified float.
Parameters:
f - the float
 o valueOf
  public static String valueOf(double d)
Returns a String object that represents the value of the specified double.
Parameters:
d - the double
 o intern
  public String intern()
Returns a String that is equal to this String but which is guaranteed to be from the unique String pool. For example:
s1.intern() == s2.intern() <=> s1.equals(s2).