The dodati() Java metoda Graditelj nizova klasa se koristi za dodavanje vrijednosti niza trenutnom nizu. U klasi StringBuilder dostupne su razne preopterećene metode append(). Ove se metode razlikuju na temelju njihovih parametara.
Sintaksa:
Pogledajmo drugačiju sintaksu metode StringBuilder append():
java boolean niz
public StringBuilder append(boolean b) public StringBuilder append(char c) public StringBuilder append(char[] str) public StringBuilder append(char[] str, int offset, int len) public StringBuilder append(CharSequence cs) public StringBuilder append(CharSequence cs, int start, int end) public StringBuilder append(double d) public StringBuilder append(float f) public StringBuilder append(int i) public StringBuilder append(long lng) public StringBuilder append(Object obj) public StringBuilder append(String str) public StringBuilder append(StringBuffer sb) public StringBuilder appendCodePoint(int codePoint)
Parametar:
Tip podataka | Parametar | Opis |
---|---|---|
Booleov | b | Booleova vrijednost koju treba dodati. |
char | c | Vrijednost znakova koja se dodaje. |
char | str | Ovo su znakovi koje treba dodati. |
int | pomaknuti | Indeks prvog znaka za dodavanje. |
int | samo | Broj znakova za dodavanje. |
CharSequence | cs | Niz znakova koji treba dodati. |
int | početak | Ovo je početni indeks podniza koji treba dodati. |
int | kraj | Ovo je krajnji indeks podniza koji treba dodati. |
dvostruko | d | Dvostruka vrijednost koju treba dodati. |
plutati | f | Vrijednost float koja se dodaje. |
int | i | Int vrijednost koju treba dodati. |
dugo | lng | Dugačka vrijednost koju treba dodati. |
Objekt | Obj | Vrijednost objekta koja se dodaje. |
Niz | str | Vrijednost niza koja se dodaje. |
StringBuffer | sb | StringBuffer koji je pridodan. |
int | codePoint | Int vrijednost Unicode kodne točke. |
Povratak:
metoda | Povratak |
---|---|
dodati (booleov b) | Vraća referencu na ovaj objekt. |
dodati (znak c) | Vraća referencu na ovaj objekt. |
dodati(char[] str) | Vraća referencu na ovaj objekt. |
dodati(char[] str, int offset, int len) | Vraća referencu na ovaj objekt. |
dodati (CharSequence cs) | Vraća referencu na ovaj dodatak. |
dodati (CharSequence cs, int početak, int kraj) | Vraća referencu na ovaj objekt. |
dodati (duplo d) | Vraća referencu na ovaj objekt. |
dodati (float f) | Vraća referencu na ovaj objekt. |
dodati (int i) | Vraća referencu na ovaj objekt. |
dodati (dugo lng) | Vraća referencu na ovaj objekt. |
dodati (objekt obj) | Vraća referencu na ovaj objekt. |
dodati (String str) | Vraća referencu na ovaj objekt. |
dodati (StringBuffer sb) | Vraća referencu na ovaj objekt. |
addCodePoint(int codePoint) | Vraća referencu na ovaj objekt. |
Iznimke:
metoda | Iznimka |
---|---|
dodati (booleov b) | DA |
dodati (znak c) | DA |
dodati(char[] str) | DA |
dodati(char[] str, int offset, int len) | IndexOutOfBoundsException |
dodati (Slijed znakova s) | DA |
dodati (CharSequence s, int početak, int kraj) | IndexOutOfBoundsException |
dodati (duplo d) | DA |
dodati (float f) | DA |
dodati (int i) | DA |
dodati (dugo lng) | DA |
dodati (objekt obj) | DA |
dodati (String str) | DA |
dodati (StringBuffer sb) | DA |
addCodePoint(int codePoint) | DA |
Verzija kompatibilnosti:
Java 1.5 i novije
Primjer Java StringBuilder append(boolean b) metode
public class StringBuilderAppendExample1 { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder('value1 '); System.out.println('builderObject :'+sb1); // appending boolean argument sb1.append(true); // print the StringBuilder after appending System.out.println('after append = ' + sb1); StringBuilder sb2 = new StringBuilder('value2 '); System.out.println('new builderObject :' + sb2); // appending boolean argument sb2.append(false); // print the string Builder after appending System.out.println('after append = ' + sb2); } }Testirajte sada
Izlaz:
builderObject :value1 after append = value1 true new builderObject :value2 after append = value2 false
Primjer Java StringBuilder append(char c) metode
public class StringBuilderAppendExample2 { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder('value1 '); System.out.println('builderObject :'+sb1); // appending char argument sb1.append('A'); // print the StringBuilder after appending System.out.println('after append = ' + sb1); StringBuilder sb2 = new StringBuilder('value2 '); System.out.println('new builderObject :' + sb2); // appending char argument sb2.append('#'); // print the string Builder after appending System.out.println('after append = ' + sb2); } }Testirajte sada
Izlaz:
promjena velikih i malih slova java
builderObject :value1 after append = value1 A new builderObject :value2 after append = value2 #
Primjer Java StringBuilder append(char[] str) metode
public class StringBuilderAppendExample3 { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder('string '); System.out.println('builderObject :'+sb1); char[] str = {'a','b','c'}; // appending char array argument sb1.append(str); // print the StringBuilder after appending System.out.println('after append = ' + sb1); } }Testirajte sada
Izlaz:
builderObject :string after append = string abc
Primjer Java StringBuilder metode append(char[] str, int offset, int len).
public class StringBuilderAppendExample4 { public static void main(String[] args){ StringBuilder sb1 = new StringBuilder('string builder '); System.out.println('builderObject :'+sb1); char[] str = {'a','b','c','d','e','f','g'}; // appending char array argument sb1.append(str,1,5); // print the StringBuilder after appending System.out.println('after append = ' + sb1); } }Testirajte sada
Izlaz:
builderObject :string builder after append = string builder bcdef
Primjer metode Java StringBuilder append(CharSequence cs).
public class StringBuilderAppendExample5 { public static void main(String[] args) { StringBuilder sb = new StringBuilder('string builder '); System.out.println('builderObject :'+sb); CharSequence cs = 'char sequence'; // appending charsequence argument sb.append(cs); // print the StringBuilder after appending System.out.println('after append = ' + sb); } }Testirajte sada
Izlaz:
builderObject :string builder after append = string builder char sequence
Primjer Java StringBuilder append(CharSequence cs, int start, int end) metode
public class StringBuilderAppendExample6 { public static void main(String[] args) { StringBuilder sb = new StringBuilder('string builder '); System.out.println('builderObject :'+sb); CharSequence cs = 'charsequence'; // appending charsequence argument start at index 4 and end at 8(exclusive) sb.append(cs,4,8); // print the StringBuilder after appending System.out.println('after append = ' + sb); } }Testirajte sada
Izlaz:
builderObject :string builder after append = string builder sequ
Primjer Java StringBuilder metode append(double d).
public class StringBuilderAppendExample7 { public static void main(String[] args) { StringBuilder sb = new StringBuilder('string builder '); System.out.println('builder :'+sb); // appending double argument sb.append(12.6543210123456789d); // print the StringBuilder after appending System.out.println('after append = ' + sb); } }Testirajte sada
Izlaz:
builder :string builder after append = string builder 12.65432101234568
Primjer Java StringBuilder metode append(float f).
public class StringBuilderAppendExample8 { public static void main(String[] args) { StringBuilder sb = new StringBuilder('string builder '); System.out.println('builder :'+sb); // appending float argument sb.append(10.65f); // print the StringBuilder after appending System.out.println('after append = ' + sb); } }Testirajte sada
Izlaz:
builder :string builder after append = string builder 10.65
Primjer Java StringBuilder append(int i) metode
public class StringBuilderAppendExample9 { public static void main(String[] args) { StringBuilder sb = new StringBuilder('append int '); System.out.println('builder :'+sb); // appending int argument sb.append(100); // print the StringBuilder after appending System.out.println('after append = ' + sb); } }Testirajte sada
Izlaz:
primjeri java koda
builder :append int after append = append int 100
Primjer Java StringBuilder append(long lng) metode
public class StringBuilderAppendExample10 { public static void main(String[] args) { StringBuilder sb = new StringBuilder('append long '); System.out.println('builder :'+sb); // appending long argument sb.append(9223372036854775807l); // print the StringBuilder after appending System.out.println('after append = ' + sb); } }Testirajte sada
Izlaz:
builder :append long after append = append long 9223372036854775807
Primjer Java StringBuilder append(Object obj) metode
public class StringBuilderAppendExample11 { public static void main(String[] args) { StringBuilder sb = new StringBuilder('append object '); System.out.println('builder :'+sb); Object obj ='objectValue'; // appending object argument sb.append(obj); // print the StringBuilder after appending System.out.println('after append = ' + sb); } }Testirajte sada
Izlaz:
builder :append object after append = append object objectValue
Primjer Java StringBuilder append(String str) metode
public class StringBuilderAppendExample12 { public static void main(String[] args) { StringBuilder sb = new StringBuilder('append string '); System.out.println('builder :'+sb); String str ='my string'; // appending string argument sb.append(str); // print the StringBuilder after appending System.out.println('after append = ' + sb); } }Testirajte sada
Izlaz:
builder :append string after append = append string my string
Primjer Java StringBuilder append(StringBuffer sb) metode
public class StringBuilderAppendExample13 { public static void main(String[] args) { StringBuilder stringBuilder = new StringBuilder('append stringBuffer '); System.out.println('builder :'+stringBuilder); StringBuffer sb =new StringBuffer('bufferValue'); // appending string buffer argument stringBuilder.append(sb); // print the StringBuilder after appending System.out.println('after append = ' + stringBuilder); } }Testirajte sada
Izlaz:
redak i stupac
builder :append stringBuffer after append = append stringBuffer bufferValue
Primjer metode Java StringBuilder appendCodePoint(int codePoint).
public class StringBuilderAppendExample14 { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder('append codePoint1 '); StringBuilder sb2 = new StringBuilder('append codePoint2 '); // appending codePoint argument sb1.appendCodePoint(64); sb2.appendCodePoint(65); // print the StringBuilder after appending System.out.println('after append : ' +sb1+' '+sb2); } }Testirajte sada
Izlaz:
after append : append codePoint1 @ append codePoint2 A