skip to Main Content

I am trying to display all the String content from an arraylist, but the size of the arraylist is unknown (using Android Studio)
So for example:

fruit = new ArrayList<>()
veg = new ArrayList<>()

After some operations, now the story arraylist contained some information, such as:

fruit = {"apple", "orange", "banana", "peach",...};

veg = {"cucumber", "spinach", "pumpkin", "broccoli",...};

I do not know how long the arraylist of fruit and veg are after the operations. But I know that the fruit and veg has the same size of arraylist. I want to store each of the list to be like:

fruitOne = fruit(0), fruitTwo = fruit(1), fruitThree = fruit(2),...

vegOne = veg(0), vegTwo = veg(1), vegThree = veg(2),...

Then I want to display them together as a String? So that I can have a string such as:

String myStore = "I am selling" + fruitOne + " and " + vegOne + "/n" + fruitTwo + " and " + vegTwo + "/n" + fruitThree + " and " + vegThree"...;

I feel like it needs to use For loops to pull each of the list one by one by calling fruit(0), fruit(1),...,fruit(i). But how do I store each of this list of fruit(0), fruit(1),...,fruit(i) independently, so that I can concatenate them together to become one String of myStore?

All the tutorial kept on taking about printLn or logd, so it can print the fruit(i) or veg(i) each for loop, but not really storing the fruit(i) or veg(i) as a variable to be used independently.

Thank you for your help!

2

Answers


  1. There are multiple ways to turn a list of words into a full sentence.
    One of which is what @deHaar mentioned.

    Using String.join( String , Iterable ); so in your case it would be something like this:

    
        List<String> story = Arrays.asList("I", " would", " like", " to", " go", " to", " the", " beach");
            
        String continuousStory  = String.join("", story);
        System.out.println(continuousStory );
    
    

    You might aswell leave out the space chars in the list and add them only when joining them to form a sentence. So visually I mean like this:

    
        // leaving out the space infront of each word in the list
        List<String> story = Arrays.asList("I", "would", "like", "to", "go", "to", "the", "beach");
        
        // adding a delimiter space character when joining the words
        String continuousStory  = String.join(" ", story);
        System.out.println(continuousStory );
    
    

    I hope this answered your question.

    Login or Signup to reply.
  2. New answer to updated question.
    Based on what you are saying in comments:

    All the tutorial kept on taking about printLn or logd, so it can print
    the fruit(i) or veg(i) each for loop, but not really storing the
    fruit(i) or veg(i) as a variable to be used independently.

    I need to store the list inside the array one by one first, so I can
    use them anytime. For example, fruitOne = fruit(0), fruitTwo =
    fruit(1), fruitThree = fruit(2), etc.

    I think you might have forgotten that the arrays of fruits and veggies are already variables you can use individually.

    You can’t create more variables dynamically at runtime.
    So for example turning an Array with 10 words cannot be turned into 10 individual variables at runtime.

    String fruitOne = fruits(0);
    String fruitTwo = fruits(1);
    String fruitThree = fruits(2);
    // no code left to assign any more variables
    

    etc… These have to be made at compile time (written declarations and assignments)

    Unless you know the maximum size of your list you could create a bunch of variables and assign them like this:

    // Assuming the arrays can be any size but  the fruit and veg has the same size of arraylist
            List<String> fruits = Arrays.asList("apple", "orange", "banana", "peach");
            List<String> veggies = Arrays.asList("cucumber", "spinach", "pumpkin", "broccoli");
    
        String fruitOne, fruitTwo, fruitThree, fruitFour, fruitFive, fruitSix;
        String vegOne, vegTwo, vegThree, vegFour, vegFive, vegSix;
    
        fruitOne = fruits.size()>0 ? fruits.get(0) : null;
        fruitTwo = fruits.size()>1 ? fruits.get(1) : null;
        fruitThree = fruits.size()>2 ? fruits.get(2) : null;
        fruitFour = fruits.size()>3 ? fruits.get(3) : null;
        fruitFive = fruits.size()>4 ? fruits.get(4) : null;
    
        vegOne = veggies.size()>0 ? veggies.get(0) :null;
        vegTwo = veggies.size()>1 ? veggies.get(1) :null;
        vegThree = veggies.size()>2 ? veggies.get(2) :null;
    
        //Now you can use them per variable but you are limited to the amount you've programmed
        String myStore = "I am selling " + fruitOne + " and " + vegOne + "n" + fruitTwo + " and " + vegTwo + "n" + fruitThree + " and " + vegThree;
        System.out.println(myStore);
    

    I’d highly advise against doing this because hardcoding things like this really isn’t necessary. It is a huge amount of code with a hard limit.

    It is also not possible to shuffle around variables within a loop you’re stuck with using a whole lot of if () {} else {} clauses. which would render the loop pointless.

    The best way to get the values from the arrays is to get them directly from the Array instead of assigning each to a variable because you are limited to how many variables you create while an array list is dynamic in size.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search