-1

I have a question regarding object creation in Powershell version 5. To simplify my question I am providing the exact Java code I want to functionally realize.

I know, Java and Powershell have nothing in common besides being object-oriented and I don't care how this can be achieved syntactically.

    Scanner scan = new Scanner(System.in);
    System.out.println("How many argument lists do you want to provide?");
    int i = Integer.parseInt(scan.next());
    List<List<String>> list = new ArrayList<>();
    for(int j = 0; j < i; j++)
    {
        list.add(new ArrayList<String>()); // this is the exact point if don't know how to substantiate in powershell
        list.get(j).add(scan.next());
        // more arguments to be entered
    }

The actual intention in Powershell is to parse user provided arguments with Read-Host and build a list of list of Strings containing them. Each list shall contain arguments for one Powershell command.

halfer
  • 19,824
  • 17
  • 99
  • 186
J.Ober
  • 199
  • 2
  • 11
  • 1
    You don't have a single question mark in your post. I don't know what you're trying to get answered. – Shawn Esterman Dec 15 '17 at 15:02
  • How can I achieve the functional equivalent of my java code in powershell? How can I instantiate an unknown amount of objects in powershell? – J.Ober Dec 15 '17 at 15:12

3 Answers3

1

You should define the question more instead of just posting java code.

But to answer: "How can I instantiate an unknown amount of objects in powershell?"

To dynamical add Strings to an Array just do sth like:

$a = @()
$a += Read-Host

In a for-loop.

If you want to add more than strings, you should read about Powershell Custom Objects .

Paxz
  • 2,959
  • 1
  • 20
  • 34
  • "The actual intention in powershell is to parse user provided arguments with Read-Host and build a list of list of Strings containing them. Each list shall contain arguments for one powershell command." I fail to understand how one could think I want to add strings to an array. I described my task very specific. I need to have a collection of collection of strings to differentiate between the context of the strings. One collection of strings is for one comandlet. They have to be seperated. – J.Ober Dec 15 '17 at 15:39
  • Like I said, if you want to add more than strings, like a list of strings, you should read into Powershell Custom Objects. The Website i linked explains the way they work pretty well. – Paxz Dec 15 '17 at 15:41
  • This can be done without creating custom objects for sure. If you don't know how, please don't answer. – J.Ober Dec 15 '17 at 15:47
0

You can use ArrayList from .NET:

$a = New-Object System.Collections.ArrayList
$a.Add("Hello")
$a.Add(42)
Palle Due
  • 5,929
  • 4
  • 17
  • 32
0

I found the solution myself. This is the exact equivalent of my java code and what I wanted to achieve:

$i = Read-Host -Prompt "How many argument lists do you want to provide?"
$listOfListsOfArguments = New-Object System.Collections.ArrayList
for ($i=1; $i -le $flag; $i++)
    {
        $Private:localParameterList = New-Object System.Collections.ArrayList
        $param = Read-Host -Prompt"argument x:"
        $localParameterList.add($param)
        #...
        $listOfListsOfArguments.add($localParameterList)
    }
J.Ober
  • 199
  • 2
  • 11