Results 1 to 8 of 8
  1. #1
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Question [SOLVED] Better way to create an array?

    I want to create an array from an another array that is an argument of a method.

    I've done this with the following code, but is there a better way to do it?

    Java Code:
    public boolean postFiles(File[] selectedFiles){
    	ArrayList<Part> partsList = new ArrayList<Part>();
    	try{
    		for (File selectedFile : selectedFiles){
    				partsList.add(new FilePart("imgFile[]", selectedFile));
    		}
    		Part[] parts = partsList.toArray(new Part[partsList.size()]);
    ...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Would System.arrayCopy work?

  3. #3
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Would System.arrayCopy work?
    No, System.arrayCopy doesn't work. Basically, I have an array of files that I want to iterate through to create objects, and I want to store each of the newly created objects in an array.

    I thought of doing:
    Java Code:
    for (File selectedFile : selectedFiles){
    	Part[] parts = new FilePart("imgFile[]", selectedFile);
    }
    But the parts variable is not accessible outside of the for loop. Any suggestions/ideas?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Declare parts outside the loop.

    Java Code:
    Part[] parts = new Part[selectedFiles.length]
    int index = 0;
    for (File selectedFile : selectedFiles){
      parts[index] = new FilePart("imgFile[]", selectedFile);
      index++;
    }
    Is Part an interface and FilePart a class that implements it?

  5. #5
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Declare parts outside the loop.

    Java Code:
    Part[] parts = new Part[selectedFiles.length]
    int index = 0;
    for (File selectedFile : selectedFiles){
      parts[index] = new FilePart("imgFile[]", selectedFile);
      index++;
    }
    Is Part an interface and FilePart a class that implements it?
    Aha! I had a complete mind-freeze yesterday. I tried declaring the "parts" variable outside the loop by:

    Java Code:
    Part[] parts = null;
    for (File selectedFile : selectedFiles){
      parts[index] = {new FilePart("imgFile[]", selectedFile)};
    }
    The above attempt did not work, so I went ahead and used an ArrayList.

    "Part" is an abstract class and FilePart is a subclass that implements it. Both of these classes are a part of Apache's HttpClient package.

    I'm curious, with such little information/code, how did you know that "Part" was an abstract class and FilePart implemented it?

    Thanks for the help, and sorry for the delayed response.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This won't work:
    Java Code:
    Part[] parts = null;
    because you first have to declare the array to use it and you have to give it a size, which you already know (see my code above), though using an ArrayList works too.
    I'm curious, with such little information/code, how did you know that "Part" was an abstract class and FilePart implemented it?
    I figured that FilePart either extended or implemented Part since you declared the array to be a Part array but instantiated the items as FilePart items.

  7. #7
    arseny is offline Member
    Join Date
    Mar 2009
    Posts
    25
    Rep Power
    0

    Default

    Part[] parts = new Part[selectedFiles.length];
    for (int i=0; i <selectedFiles.length; i++)
    parts[i] = new FilePart("imgFile[]", selectedFiles[i]);

  8. #8
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This won't work...you first have to declare the array to use it and you have to give it a size, which you already know (see my code above), though using an ArrayList works too.
    You're right; arrays need to be declared and instantiated:
    Java Code:
    String[] strArray = new String[5];
    or declared and assigned values:
    Java Code:
    String[] strArray = {"How", "Are", "You"};
    Before I saw your post, I was trying to do the latter while making the variable accessible outside of the for-loop. When I saw your post, I realized my mistake.

    Best regards,
    Dan

Similar Threads

  1. Create an array of Vectors
    By nwboy74 in forum Advanced Java
    Replies: 1
    Last Post: 11-12-2008, 09:00 AM
  2. How do I create an array with every cycle of a loop?
    By blackhole8746 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:49 AM
  3. Replies: 0
    Last Post: 04-23-2008, 09:15 PM
  4. Replies: 0
    Last Post: 04-14-2008, 09:43 PM
  5. How do I create an Array of Images
    By wco5002 in forum New To Java
    Replies: 3
    Last Post: 03-21-2008, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •