0% found this document useful (0 votes)
30 views4 pages

Arrays:: DS Basics

The document discusses different data structures and methods in Java including arrays, switch statements, converting between data types like char to string, string to char array, and list to array. Arrays, populating arrays, declaring char arrays, switch statements, and approaches to convert char to string are covered. Converting string to char array using toCharArray and converting lists to arrays using toArray are also summarized.

Uploaded by

mukul_mangla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views4 pages

Arrays:: DS Basics

The document discusses different data structures and methods in Java including arrays, switch statements, converting between data types like char to string, string to char array, and list to array. Arrays, populating arrays, declaring char arrays, switch statements, and approaches to convert char to string are covered. Converting string to char array using toCharArray and converting lists to arrays using toArray are also summarized.

Uploaded by

mukul_mangla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

DS Basics:

Arrays:
Declare an array of size 10.
Int[] seq = new int[10];

Populate the array:


for(int i = 0; i < seq.length; ++i){
seq[i] = i;
}
Declare and fill char Array
char[] s = {‘a’,’b’,’c’,’d’}

Switch Statement

class HelloWorld {
    public static void main(String args[]) {
        int i = 0;

        switch(i){
            case 0:
                System.out.println("int = 0");
                break;
            default:
                System.out.println("int != 0");
        }
    }
}
How To Convert Char To String In Java?

Approach #1

public class Sample {


public static final void main(String[] args){
String string1 = Character.toString('c');
}
}

Approach #2
Here is another way, which is equivalent to the one above. You can create an instance of the
class Character using its constructor. This is called boxing. Boxing means wrapping a primitive
type with its equivalent object type.

Once you wrap the character, you can call the toString() method to get the String. See below
how this works:

String string2 = new Character('c').toString();

Approach #3
The String class also offer a way to convert char to string. You can use the static method
valueOf() passing as a parameter(or argument), the character you want to convert. As a
result, you will get exactly the same character saved in a string variable. See below how this
will work:

public class Sample {


public static final void main(String[] args){
String string3 = String.valueOf('c');
}
}
String to char array java
Another very often used data conversion is converting a string to a char array. This enables
you to loop through each character. The String class provides a method for exactly this
purpose. The toCharArray() method, will return a char array containing all string letter,
without changing its order. See example below illustrates how the toCharArray() method
works:

public class Sample{


public static final void main(String[] args) {
String text = "Hey there";
// Convert string to char array
char[] array = text.toCharArray();
for(char c: array){
System.out.println(c);
}
}
}

Java list to array


Converting a list to an array in java is a common operation. For this purpose, the List class has a
method called toArray() which will return an array. As a parameter, you need to pass the
elements type. See how this works below:

import java.util.Arrays;
import java.util.List;
public class Sample {

public static void main(String[] args) {


List<Integer> list = Arrays.asList(54, 234, 64, 23);
Integer[] array = list.toArray(new Integer[0]);
for(Integer i:array) {
System.out.println(i);
}
}
}

You might also like