Download Arrays

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Arrays
Creating an Array
1. Declaring the array
Form1: type arrayname[ ];
E.g int number[];
Form2: type[ ] arrayname;
E.g float[] marks;
2. Creating memory locations
Syntax: -> arrayname = new type[size]
e.g number=new int[10];
3. Initialization of Arrays i.e. putting values into the memory locations.
Form1: arrayname[subscript]=value;
e.g number[0]=35; …….. number[9]=67;
Form2: type arrayname[ ]={list of values};
E.g int number[]={34,45,67,23};
Array length
-arrays store the allocated size in a variable named length.
-to find length
int assize=number.length;
Two-dimensional arrays
1. type arrayname[ ][ ];
2. arrayname=new type[rows][cols];
e.g int number[][];
number=new int[3][4];
Variable Size Arrays
int x[][] = new int[3][];
x[0]=new int[2];
x[1]=new int[4];
x[2]=new int[3];
Strings(java.lang)
- They are class objects and implemented using two classes
o String
o StringBuffer
- It is not a character array and not NULL terminated.
String Class
Creates strings of fixed length
Manipulation of strings
Replace characters
StringBuffer class
Creates strings of flexible length
Manipulation and modification of strings.
Insert characters and substring in the middle
of a string or append to the end of string.
Creating strings
Syntax:
String stringname;
Stringname = new String(“string”);
e.g String firstname;
firstname= new String(“Anil”);
Length of the strings-> int variablename=stringname.length();
e.g int m=firstname.length();
String Arrays
String itemArray[]=new String[3];
String class
//Constructors
public String();
public String(byte ascii[], int hibyte);
public String(byte ascii[], int hibyte, int off, int count);
public String(char value[]);
public String(char value[], int off, int count);
public String(String value);
public String(StringBuffer buffer);
// Methods
public char charAt(int index);
public int compareTo(String anotherString);
public String concat(String str);
public static String copyValueOf(char data[]);
public static String copyValueOf(char data[], int off, int count);
public boolean endsWith(String suffix);
public boolean equals(Object anObject);
public boolean equalsIgnoreCase(String anotherString);
public void getBytes(int srcBegin, int srcEnd, byte dst[], int
dstBegin);
public void getChars(int srcBegin, int srcEnd, char dst[], int
dstBegin);
public int indexOf(int ch);
public int indexOf(int ch, int fromIndex);
public int indexOf(String str);
public int indexOf(String str, int fromIndex);
public String intern();
public int lastIndexOf(int ch);
public int lastIndexOf(int ch, int fromIndex);
public int lastIndexOf(String str);
public int lastIndexOf(String str, int fromIndex);
public boolean regionMatches(boolean ignoreCase, int toffset, String
other,int ooffset, int len);
public int length();
public boolean regionMatches(int toffset, String other, int ooffset,
int len);
public String replace(char oldChar, char newChar);
public boolean startsWith(String prefix);
public boolean startsWith(String prefix, int toffset);
public String substring(int beginIndex);
public String substring(int beginIndex, int endIndex);
public
public
public
public
public
public
public
public
public
public
public
public
public
public
char[]
String
String
String
String
static
static
static
static
static
static
static
static
static
toCharArray();
toLowerCase();
toString();
toUpperCase();
trim();
String valueOf(boolean b);
String valueOf(char c);
String valueOf(char data[]);
String valueOf(char data[], int off, int count);
String valueOf(double d);
String valueOf(float f);
String valueOf(int i);
String valueOf(long l);
String valueOf(Object obj);
String S1,S2;
S1=new String(“Vellore Instptute of Techonology”);
S2=new String(“Vellore”);
Methods
S2=S1.toLowerCase()
S2=S1.toUpperCase()
S2=S1.replace(‘p’,’i’)
S2=S1.trim()
Task performed
Converts the string s1 to lowercase
Converts the string s1 to uppercase
Replace all appearances of x with y
Remove white spaces at the beginning and end of the
string S1
S1.equals(S2)
Returns ‘true’ if S1is equal to S2;
S1.equalsIgnoreCase(S2) Returns ‘true if S1=S2,ignoring the case
S1.length()
Gives the length of S1
S1.charAt(n)
Gives nth character of S1
S1.compareTo(S2)
Returns negative if S1<S2,positive if S1>S2, and zero if
S1 is equal to S2
S1.concat(S2)
Concatenates S1 and S2
S1.substring(n)
Gives substring starting from nth character
S1.substring(n,m)
Gives substring starting from nth character up to mth (not
including mth
String.valueOf(p)
Creates a string object of the parameter p(simple type or
object)
p.toString()
Creates a string representation of object ‘p’
S1.indexOf(‘V’)
Gives the position of the first occurrence of ‘V’ in the
string S1
S1.indexOf(‘V’,n)
Gives the position of ‘V’ that occurs after nth position in
the string S1
StringBuffer class
public StringBuffer();
public StringBuffer(int length);
public StringBuffer(String str);
// Methods
public StringBuffer append(boolean b);
public StringBuffer append(char c);
public StringBuffer append(char str[]);
public StringBuffer append(char str[], int off, int len);
public StringBuffer append(double d);
public StringBuffer append(float f);
public StringBuffer append(int i);
public StringBuffer append(long l);
public StringBuffer append(Object obj);
public StringBuffer append(String str);
public int capacity();
public char charAt(int index);
public void ensureCapacity(int minimumCapacity);
public void getChars(int srcBegin, int srcEnd, char dst[], int
dstBegin);
public StringBuffer insert(int off, boolean b);
public StringBuffer insert(int off, char c);
public StringBuffer insert(int off, char str[]);
public StringBuffer insert(int off, double d);
public StringBuffer insert(int off, float f);
public StringBuffer insert(int off, int i);
public StringBuffer insert(int off, long l);
public StringBuffer insert(int off, Object obj);
public StringBuffer insert(int off, String str);
public int length();
public StringBuffer reverse();
public void setCharAt(int index, char ch);
public void setLength(int newLength);
public String toString();
Method
S1.setCharAt(n,’V’)
S1.append(S2)
S1.insert(n,S2)
S1.setLength(n)
StringManipulation.java
Task
Modifies the nth character of ‘V’
Appends the string S2 to S1 at the end
Inserts the string S2 at position n of the
String S1
Sets the length of the string S1 to n.
If n<S1.length()S1 is truncated
If n>S1.length() zeros are added to S1.
import java.lang.*;
class StringManupulation
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer(“Object language”);
System.out.println(“Original String:” + str);
//obtaining the string length
System.out.println(“Length of string:” + str.length());
//Accessing characters in a string
for(int i=0;i<str.length();i++)
{
int p=i+1;
System.out.println(“character at position:” + p + “is” + str.charAt(i));
}
//inserting a string in the middle
String astring=new String(str.toString());
int pos=astring.indexOf(“ language”);
str.insert(pos,”oriented “);
System.out.println(“Modified string:” + str);
//Modifying characters
str.setCharAt(6,’-‘);
System.out.println(“String now:”+ str);
//appending a string at the end
str.append(“ improves security”);
System.out.println(“Appended string:”+str);
}
}
StringOrdering.java
class StringOrdering
{
String name[]={“Chennai”, ”New Delhi”, ”Ahmedabad”, ”Goa”,”Mumbai”};
public static void main(String args[])
{
int size = name.length;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
// swap the strings
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}
for(int i=0;i<size;i++)
{
System.out.println(name[i]);
}
}
}
public class Main {
/**
* Converts a String to a character array
*/
public void convertStringToCharArray() {
String str = "Abcdef";
char[] cArray = str.toCharArray();
for (char c : cArray)
System.out.println(c);
}
/**
* Starts the program
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main().convertStringToCharArray();
}
}
The output from the executed code:
A
b
c
d
e
f
Related documents