2

I am newbie in Java.please don't put -ve vote. I just want to know. I have following data:

[john-london-4455-null,mark-newyork-null-null,abishek-null-2244-java developer...may be more]

may be more these data dynamic which get from web.

Now I want create in this case, three array list.It is depend upon the data getting from web.

Edit:

My intention is, I want to create

array1= john, mark, abishek
array2= london, newyork,null
array3=4455,null,2244
array4=null,null, java developer
.
.
. 

may be more depend upon data.

5
  • I didn't understand your question. If you give more details I'll tell you the perfect solution. Commented Feb 11, 2014 at 4:08
  • you wanna put all data in a array . Commented Feb 11, 2014 at 4:08
  • Can you please tell in which format you are getting response from server ? Commented Feb 11, 2014 at 4:44
  • I am getting data like this- [john-london-4455-null,mark-newyork-null-null,abishek-null-2244-java developer]. Commented Feb 11, 2014 at 4:53
  • So are you getting john-london-4455-null as a single String ? What's your main problem, Extracting value from the String or adding in Array ? Commented Feb 11, 2014 at 5:09

4 Answers 4

1

It looks like your data is some sort of Array of Objects with text separated by hyphens. You can parse this into lists using the following code:

String data = "[[john-london-4455-null],[mark-newyork-null-null],[abishek-null-2244-java developer]]";

//first, remove the first and last square brackets
data = data.substring(1, data.length()-1);
//now separate each object by splitting on the commas
String[] parts = data.split(",");

//create the array that will store all your array data
String[][] lists = new String[parts.length][];

//now create each list and store it in your array
for (int i = 0; i < parts.length; i++) {
    String str = parts[i].substring(1, parts[i].length()-1);
    lists[i] = str.split("-");
}

Now you have the lists object to work with. This is more efficient than using List<String> too.

Sign up to request clarification or add additional context in comments.

2 Comments

I have above data in arraylist not string. This data in arraylist->>[john-london-4455-null,mark-newyork-null-null,abishek-null-2244-java developer]. So how to split these.
Thank you Phil, It is very closer of my problem.
1

Why don't you create an Utility class like

public class PersonInfo{

  String name;
  String loc;
  String add;
}

And create an ArrayList<PersonInfo> In this case you don't have to create separate ArrayList

Comments

1

Below is the code snippet I have written as per my understanding of your problem. I tried to split the ArrayList data into array. Then using simple for loops insert the split data into respective lists.

  String[] listToArray;
        List<String> one  = new ArrayList<>();
        List<String> two  = new ArrayList<>();

        List<String> l = new ArrayList<>();
        l.add("1-name1-addr1");
        l.add("2-name2-addr2");

// l contains the data as you mentioned 

for (Iterator<String> iterator = l.iterator(); iterator.hasNext();) {
            String string = (String) iterator.next();


    listToArray = string.split("-");

            for(int i =0; i<listToArray.length; i+=3){
                one.add(listToArray[i]);
            }

            for(int i =1; i<listToArray.length; i+=3){
                two.add(listToArray[i]);
            }

        }

        System.out.println(one);
        System.out.println(two);
    }

If you needed the output as an array just call toArray() method on individual lists.

PS: Things would become more easier if you can opt for beans as suggest by few here.

Comments

1

create PersonInfo Bean class

class PersonInfo
{
    String name,loc,add;
    Data(String name,String loc,String add)
    {
        this.name=name;
        this.loc=loc;
        this.add=add;
    }
}

And create an ArrayList<PersonInfo>

List<PersonInfo> list = new ArrayList<PersonInfo>();
list.add(new PersonInfo("john", "london", "4455"));
list.add(new PersonInfo("abishek", "mumbai", "2244"));
list.add(new PersonInfo("mark", "newyork", "3366"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.