Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

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

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){
    		list.add(matcher.group());
		obj = new Object[]{matcher.group()};
	}

    	//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...

Regards,
0
epifanio67
Asked:
epifanio67
  • 2
1 Solution
 
CEHJCommented:
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?
0
 
epifanio67Author Commented:
I see what you mean....
Thx for your help...
Regards,
0
 
CEHJCommented:
:)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now