Java: why can I not add values to my Object[ ]?

Posted on 2012-08-21
Last Modified: 2012-08-21
Hello Experts,

Why can I not add values to my Obj [ ] ?

public static void main(String args[]) {

	Pattern p = Pattern.compile("Sip\\w*");

	String file = "message SipRoute test in SipUser " + "\n"
		+ "receipt SipCall at time SipUpdate " + "\n"
		+ "message SipReg during SipDiv " + "\n";

	ArrayList<String> list = new ArrayList<String>();
	Object obj[] = null;

    	Matcher matcher = p.matcher(file);
    	while(matcher.find() == true){
		obj = new Object[]{};

    	//prints arraylist ok
	for (int x = 0; x < list.size(); x++) {
		System.out.println("myArray: " + list.get(x));

	//fails to add to Obj array - why?
	for (int x = 0; x < obj.length; x++) {
		System.out.println("obj: " + obj[x]);

Open in new window

In advance, thank you for your help...

Question by:epifanio67
    LVL 86

    Accepted Solution

    All you're doing is (re)creating an array containing one Object at each iteration of the loop. Why not just turn your List into an array if you really need one?

    Author Closing Comment

    I see what you mean....
    Thx for your help...
    LVL 86

    Expert Comment


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    twoTwo  challenge 35 74
    linearIn  challenge 23 55
    Convert BufferedReader to File 1 49
    What is JNDI datasource in spring 1 21
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now